Java Date and Calendar
Calendar.add() vs Calendar.roll()
- calendar add vs roll
- Java 日付時刻・カレンダー
Calendar.roll()
はうるう秒にバグがある
Calendar.roll()
は使わないほうがいい
start time & end time
After Java8
public static Date getEndOfDay(Date date) {
LocalDateTime localDateTime = dateToLocalDateTime(date);
LocalDateTime endOfDay = localDateTime.with(LocalDateTime.MAX);
return localDateTimeToDate(endOfDay);
}
public static Date getStartOfDay(Date date) {
LocalDateTime localDateTime = dateToLocalDateTime(date);
LocalDateTime startOfDay = localDateTime.with(LocalDateTime.MIN);
return localDateTimeToDate(startOfDay);
}
private static Date localDateTimeToDate(LocalDateTime startOfDay) {
return Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
}
private static LocalDateTime dateToLocalDateTime(Date date) {
return LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
}
Java 7 and Earlier
With Apache Commons
public static Date getEndOfDay(Date date) {
return DateUtils.addMilliseconds(DateUtils.ceiling(date, Calendar.DATE), -1);
}
public static Date getStartOfDay(Date date) {
return DateUtils.truncate(date, Calendar.DATE);
}
Without Apache Commons
public Date getEndOfDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 999);
return calendar.getTime();
}
public Date getStartOfDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}