public class DateUtil { // 获得某天最大时间 2018-03-20 23:59:59 public static Date getEndOfDay(Date date) {
Calendar calendarEnd = Calendar.getInstance();
calendarEnd.setTime(date);
calendarEnd.set(Calendar.HOUR_OF_DAY, 23);
calendarEnd.set(Calendar.MINUTE, 59);
calendarEnd.set(Calendar.SECOND, 59);
//防止mysql自动加一秒,毫秒设为0
calendarEnd.set(Calendar.MILLISECOND, 0);
return calendarEnd.getTime();
} // 获得某天最小时间 2018-03-20 00:00:00 public static Date getStartOfDay(Date date) {
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault()); LocalDateTime startOfDay = localDateTime.with(LocalTime.MIN); return Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
} }