package com.xyz.util;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class DateTimeUtils {
public static void main(String[] args) {
Date date = new Date();
date.setHours(23);
System.out.println("prev whole hour, millies: "+toWholeHour(date,-1)+", "+toWholeHour(date,-1).getTime());
System.out.println("curr whole hour, millies: "+toWholeHour(date,0)+", "+toWholeHour(date,0).getTime());
System.out.println("next whole hour, millies: "+toWholeHour(date,1)+", "+toWholeHour(date,1).getTime());
System.out.println("prev whole minute, millies: "+toWholeMinute(date,-1)+", "+toWholeMinute(date,-1).getTime());
System.out.println("curr whole minute, millies: "+toWholeMinute(date,0)+", "+toWholeMinute(date,0).getTime());
System.out.println("next whole minute, millies: "+toWholeMinute(date,1)+", "+toWholeMinute(date,1).getTime());
}
public static Date toWholeHour(Date d, int beforeOrAfter) {
Calendar c = new GregorianCalendar();
c.setTime(d);
c.add(Calendar.HOUR, beforeOrAfter);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
return c.getTime();
}
public static Date toWholeMinute(Date d, int beforeOrAfter) {
Calendar c = new GregorianCalendar();
c.setTime(d);
c.add(Calendar.MINUTE, beforeOrAfter);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
return c.getTime();
}
}