我需要使用 java.time API 查找下个月第二个星期日的日期。我是时间 API 的新手。我试过这段代码:
LocalDate current=LocalDate.now();
这是给我当前的日期,但是 LocalDate
没有任何这样的方法可以让我获得 nextMonth 或类似的东西。请建议。我只需要使用时间 API。
最佳答案
这可以使用 TemporalAdjuster
来完成,如下所示:
LocalDateTime now = LocalDateTime.now();
System.out.println("First day of next month: " + now.with(TemporalAdjusters.firstDayOfNextMonth()));
System.out.println("First Friday in month: " + now.with(TemporalAdjusters.firstInMonth(DayOfWeek.FRIDAY)));
// Custom temporal adjusters.
TemporalAdjuster secondSundayOfNextMonth = temporal -> {
LocalDate date = LocalDate.from(temporal).plusMonths(1);
date = date.with(TemporalAdjusters.dayOfWeekInMonth(2, DayOfWeek.SUNDAY));
return temporal.with(date);
};
System.out.println("Second sunday of next month: " + now.with(secondSundayOfNextMonth));
关于java - 使用时间 API 获取下个月的特定日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51550365/