Returns a Date set just to Noon, to the closest possible millisecond of the day in Java
Description
The following code shows how to returns a Date set just to Noon, to the closest possible millisecond of the day.
Example
// ww w . j a v a 2 s .c o m
import java.util.Calendar;
import java.util.Date;
public class Main {
public static Date getNoonOfDay(Date day, Calendar cal) {
if (day == null)
day = new Date();
cal.setTime(day);
cal.set(Calendar.HOUR_OF_DAY, 12);
cal.set(Calendar.MINUTE, cal.getMinimum(Calendar.MINUTE));
cal.set(Calendar.SECOND, cal.getMinimum(Calendar.SECOND));
cal.set(Calendar.MILLISECOND, cal.getMinimum(Calendar.MILLISECOND));
return cal.getTime();
}
public static void main(String[] argv) {
System.out.println(getNoonOfDay(new Date(), Calendar.getInstance()));
}
}
The code above generates the following result.