无需手动进行任何计算.
Returns the next day-of-week adjuster, which adjusts the date to the first occurrence of the specified day-of-week after the date being adjusted.
public static void main(String[] args) {
LocalDateTime dateTime = LocalDateTime.now();
LocalDateTime nextMonday = dateTime.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
System.out.println(nextMonday);
}
此代码将根据当前日期返回下一个星期一.
使用静态导入,这使代码更容易阅读:
LocalDateTime nextMonday = dateTime.with(next(MONDAY));
请注意,如果当前日期已经在星期一,则此代码将返回下一个星期一(即下周的星期一).如果要在这种情况下保留当前日期,可以使用nextOrSame(dayOfWeek).