序号 | 方法 | 描述 |
1 | dayOfWeekInMonth | 返回同一个月中每周的第几天 |
2 | firstDayOfMonth | 返回当月的第一天 |
3 | firstDayOfNextMonth | 返回下月的第一天 |
4 | firstDayOfNextYear | 返回下一年的第一天 |
5 | firstDayOfYear | 返回本年的第一天 |
6 | firstInMonth | 返回同一个月中第一个星期几 |
7 | lastDayOfMonth | 返回当月的最后一天 |
8 | lastDayOfNextMonth | 返回下月的最后一天 |
9 | lastDayOfNextYear | 返回下一年的最后一天 |
0 | lastDayOfYear | 返回本年的最后一天 |
11 | lastInMonth | 返回同一个月中最后一个星期几 |
12 | next / previous | 返回后一个/前一个给定的星期几 |
13 | nextOrSame / previousOrSame | 返回后一个/前一个给定的星期几,如果这个值满足条件,直接返回 |
LocalDateTime now = LocalDateTime.now();
System.out.println("当前时间:" + now + "======>" + now.getDayOfWeek());
System.out.println("下一个周一:" + now.with(TemporalAdjusters.next(DayOfWeek.MONDAY)));
System.out.println("上一个周一:" + now.with(TemporalAdjusters.previous(DayOfWeek.MONDAY)));
System.out.println("下一个周五:" + now.with(TemporalAdjusters.nextOrSame(DayOfWeek.FRIDAY)));
System.out.println("上一个周五:" + now.with(TemporalAdjusters.previousOrSame(DayOfWeek.FRIDAY)));
System.out.println("本月最后一个周五:" + now.with(TemporalAdjusters.lastInMonth(DayOfWeek.FRIDAY)));
System.out.println("本月第一个周五:" + now.with(TemporalAdjusters.firstInMonth(DayOfWeek.FRIDAY)));
System.out.println("本月第一天:" + now.with(TemporalAdjusters.firstDayOfMonth()));
System.out.println("本月最后一天:" + now.with(TemporalAdjusters.lastDayOfMonth()));
System.out.println("下月的第一天:" + now.with(TemporalAdjusters.firstDayOfNextMonth()));
System.out.println("今年的第一天:" + now.with(TemporalAdjusters.firstDayOfYear()));
System.out.println("今年的最后一天:" + now.with(TemporalAdjusters.lastDayOfYear()));
System.out.println("下一年的第一天:" + now.with(TemporalAdjusters.firstDayOfNextYear()));
System.out.println("本月的第二个周五:" + now.with(TemporalAdjusters.dayOfWeekInMonth(2,DayOfWeek.FRIDAY)));
System.out.println("两周后:" + now.with(TemporalAdjusters.ofDateAdjuster(date -> date.plusWeeks(2))));
/*打印结果:
当前时间:2020-04-03T11:32:59.871======>FRIDAY
下一个周一:2020-04-06T11:32:59.871
上一个周一:2020-03-30T11:32:59.871
下一个周五:2020-04-03T11:32:59.871
上一个周五:2020-04-03T11:32:59.871
本月最后一个周五:2020-04-24T11:32:59.871
本月第一个周五:2020-04-03T11:32:59.871
本月第一天:2020-04-01T11:32:59.871
本月最后一天:2020-04-30T11:32:59.871
下月的第一天:2020-05-01T11:32:59.871
今年的第一天:2020-01-01T11:32:59.871
今年的最后一天:2020-12-31T11:32:59.871
下一年的第一天:2021-01-01T11:32:59.871
本月的第二个周五:2020-04-10T11:32:59.871
两周后:2020-04-17T11:32:59.871
*/
// 自动以时间矫正器,获取下一个工作日
LocalDateTime nextWorkDay = LocalDateTime.now().with(tempDateTime -> {
LocalDateTime localDateTime = (LocalDateTime) tempDateTime;
DayOfWeek dayOfWeek = localDateTime.getDayOfWeek();
if (dayOfWeek.equals(DayOfWeek.FRIDAY)) {
return localDateTime.plusDays(3);
} else if (dayOfWeek.equals(DayOfWeek.SATURDAY)) {
return localDateTime.plusDays(2);
} else {
return localDateTime.plusDays(1);
}
});
System.out.println("下一个工作日:" + nextWorkDay);