📌  相关文章
📜  Java中的 TemporalAdjusters nextOrSame() 方法及示例

📅  最后修改于: 2022-05-13 01:55:01.525000             🧑  作者: Mango

Java中的 TemporalAdjusters nextOrSame() 方法及示例

TemporalAdjusters 类nextOrSame(DayOfWeek)方法用于返回下一个或相同的 DayOfWeek TemporalAdjuster 对象,该对象可用于获取新的 Date 对象,该对象是具有相同 DayOfWeek 指定为参数的下一个或相同日期,来自应用此 TempotralAdjuster 的任何 Date 对象。

句法:

public static TemporalAdjuster
       nextOrSame(DayOfWeek dayOfWeek)

参数:此方法接受dayOfWeek可用于获取新的 Date 对象,该对象是具有相同 DayOfWeek 的下一个或相同日期

返回值:此方法返回下一个或相同的 DayOfWeek 调整器。

下面的程序说明了 TemporalAdjusters.nextOrSame() 方法:
方案一:

// Java program to demonstrate
// TemporalAdjusters.nextOrSame()
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // get TemporalAdjuster with the nextOrSame in
        // month adjuster
        TemporalAdjuster temporalAdjuster
            = TemporalAdjusters.nextOrSame(DayOfWeek.WEDNESDAY);
  
        // using adjuster for local date time
        LocalDate localDate
            = LocalDate.of(2020, 10, 31);
        LocalDate nextOrSameDOW
            = localDate.with(temporalAdjuster);
  
        // print
        System.out.println("Next or same date having "
                           + "WEDNESDAY for localdate "
                           + localDate + " is: "
                           + nextOrSameDOW);
    }
}
输出:
Next or same date having WEDNESDAY for localdate 2020-10-31 is: 2020-11-04

方案二:

// Java program to demonstrate
// TemporalAdjusters.nextOrSame() method
  
import java.time.*;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // get TemporalAdjuster with the
        // nextOrSame day of week adjuster
        TemporalAdjuster temporalAdjuster
            = TemporalAdjusters
                  .nextOrSame(
                      DayOfWeek.THURSDAY);
  
        // using adjuster for local date time
        LocalDate localDate
            = LocalDate.of(2020, 02, 27);
        LocalDate nextOrSameDOW
            = localDate.with(temporalAdjuster);
  
        // print
        System.out.println("Next or same date having "
                           + "WEDNESDAY for localdate "
                           + localDate + " is: "
                           + nextOrSameDOW);
    }
}
输出:
Next or same date having WEDNESDAY for localdate 2020-02-27 is: 2020-02-27

参考资料:https: Java Java.time.DayOfWeek)