时间
2019 09 19 00:00:00 — 2019 09 19 23:59:59
public class Utils {
public static void main(String[] args) {
Date date = new Date();
System.out.println("今天开始时间:" + getStartOfDay(date));
System.out.println("今天结束时间:" + getEndOfDay(date));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(getStartOfDay(date)));
System.out.println(sdf.format(getEndOfDay(date)));
}
public static Date getEndOfDay(Date date) {
LocalDateTime localDateTime =
LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX);
return Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
}
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());
}
}
结果