java8 获取某天最大(23:59:59)和最小时间(00:00:00)

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());
} }

 

posted @ 2018-03-20 19:32  枫树湾河桥  阅读(8957)  评论(0编辑  收藏  举报
Live2D