Below snippet return the start time of next hour.
public static long getStartOfNextHour(long now) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date(now));
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.add(Calendar.HOUR, 1);
return calendar.getTime().getTime();
}
Find the below working application.
GetStartOfNextHour.java
package com.sample.app.time;
import java.util.Calendar;
import java.util.Date;
public class GetStartOfNextHour {
public static long getStartOfNextHour(long now) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date(now));
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.add(Calendar.HOUR, 1);
return calendar.getTime().getTime();
}
public static void main(String args[]) {
Date today = new Date();
long startOfNextHourInMillis = getStartOfNextHour(today.getTime());
Date startOfNextHour = new Date(startOfNextHourInMillis);
System.out.println("today : " + today);
System.out.println("startOfNextHour : " + startOfNextHour);
}
}
Output
today : Sat Feb 26 10:21:25 IST 2022 startOfNextHour : Sat Feb 26 11:00:00 IST 2022
No comments:
Post a Comment