Java中的 TemporalAdjusters next() 方法及示例
TemporalAdjusters 类的next(DayOfWeek)方法用于返回下一个星期几 TemporalAdjuster 对象,该对象可用于获取新的 Date 对象,该对象是具有相同匹配 DayOfWeek 的下一个日期,作为从任何参数传递的参数应用此 TempotralAdjuster 的日期对象。
句法:
public static TemporalAdjuster next(DayOfWeek dayOfWeek)
参数:此方法接受dayOfWeek可用于获取新的 Date 对象,该对象是具有相同匹配 DayOfWeek 的下一个日期。
返回值:该方法返回星期调整器的第二天,不为空。
下面的程序说明了 TemporalAdjusters.next() 方法:
方案一:
// Java program to demonstrate
// TemporalAdjusters.next()
import java.time.*;
import java.time.temporal.*;
public class GFG {
public static void main(String[] args)
{
// get TemporalAdjuster with
// the next in month adjuster
TemporalAdjuster temporalAdjuster
= TemporalAdjusters.next(
DayOfWeek.WEDNESDAY);
// using adjuster for local date time
LocalDate localDate
= LocalDate.of(1998, 10, 31);
LocalDate nextDOW
= localDate.with(temporalAdjuster);
// print
System.out.println(
"next day of the week having"
+ " WEDNESDAY for localdate "
+ localDate + " is: "
+ nextDOW);
}
}
输出:
next day of the week having WEDNESDAY for localdate 1998-10-31 is: 1998-11-04
方案二:
// Java program to demonstrate
// TemporalAdjusters.next() method
import java.time.*;
import java.time.temporal.*;
public class GFG {
public static void main(String[] args)
{
// get TemporalAdjuster with the
// next day of week adjuster
TemporalAdjuster temporalAdjuster
= TemporalAdjusters.next(
DayOfWeek.THURSDAY);
// using adjuster for local date time
LocalDate localDate
= LocalDate.of(2029, 12, 11);
LocalDate nextDOW
= localDate.with(temporalAdjuster);
// print
System.out.println(
"next day of the week having"
+ " THURSDAY for localdate "
+ localDate + " is: "
+ nextDOW);
}
}
输出:
next day of the week having THURSDAY for localdate 2029-12-11 is: 2029-12-13
参考:https: Java Java.time.DayOfWeek)