Set date to the end of a day
import java.util.Calendar;
import java.util.Date;
public class Main {
public static void main(String[] argv) {
System.out.println(getEndOfDay(new Date()));
}
public static Date getEndOfDay(Date day) {
return getEndOfDay(day, Calendar.getInstance());
}
public static Date getEndOfDay(Date day, Calendar cal) {
if (day == null)
day = new Date();
cal.setTime(day);
cal.set(Calendar.HOUR_OF_DAY, cal.getMaximum(Calendar.HOUR_OF_DAY));
cal.set(Calendar.MINUTE, cal.getMaximum(Calendar.MINUTE));
cal.set(Calendar.SECOND, cal.getMaximum(Calendar.SECOND));
cal.set(Calendar.MILLISECOND, cal.getMaximum(Calendar.MILLISECOND));
return cal.getTime();
}
}
Home
Java Book
Runnable examples
Java Book
Runnable examples
Date Set:
- Set Date to Noon
- Set Date to the first millisecond of the day, just after midnight.
- Set Date to the first millisecond of the month, just after midnight.
- Set Date to the last millisecond of the day, just before midnight.
- Set Date to the last millisecond of the minute.
- Set Date to the last millisecond of the month, just before midnight.
- Set date to clear the time values
- Set date to the end of the day
- Set Date to the start of the day
- Set date to the end of a day
- Set date to the start of an hour
- Set date to then end of an hour
- Set date to the start of a minute
- Set date to the end of a minute
- Set Calendar to Mid night
- Set year, month and day to a date
- Roll java.util.Date back and forth