java8 LocalDateTime/LocalDate 获取各种日期类型

获取当前时间:

LocalDateTime.now()

获取当天:

LocalDate.now()

获取本周星期一至星期天

LocalDate.now().with(DayOfWeek.MONDAY)
...
...
LocalDate.now().with(DayOfWeek.SUNDAY)

获取星期一至星期天中文:

now.with(DayOfWeek.MONDAY).getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.CHINA);
...
...
now.with(DayOfWeek.SUNDAY).getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.CHINA);

获取一天开始时间

final LocalDateTime startOfDay = LocalDate.now().atStartOfDay();

获取一天结束时间

final LocalDate now = LocalDate.now();
final LocalDateTime endTime = LocalDateTime.of(now .getYear(), now .getMonth(), now .getDayOfMonth(), 23, 59, 59);

获取前一天/后一天

final LocalDateTime now = LocalDateTime.now();
final LocalDateTime afterDay  = now.plusDays(1);  //增加一天
final LocalDateTime beforeDay = now.minusDays(1); //减少一天

获取当月开始时间 / 结束时间

final LocalDate now = LocalDate.now();
final LocalDateTime startOfDay = now.atStartOfDay();
final LocalDateTime fistDayOfMonth = startOfDay.with(TemporalAdjusters.firstDayOfMonth());
final LocalDateTime endOfDay = now.plusDays(1L).atStartOfDay().plusNanos(-1L)
final LocalDateTime endDayOfMonth = endOfDay.with(TemporalAdjusters.lastDayOfMonth()

获取当年开始时间 / 结束时间

final LocalDate now = LocalDate.now();
final LocalDateTime startOfDay = now.atStartOfDay();
final LocalDateTime firstDayOfYear = startOfDay.with(TemporalAdjusters.firstDayOfYear())
final LocalDateTime lastDayOfYear = startOfDay.with(TemporalAdjusters.lastDayOfYear())

 

posted @ 2021-06-16 19:44  Dragon_Smile  阅读(2306)  评论(0编辑  收藏  举报