当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java TemporalAdjusters nextOrSame()用法及代码示例


TemporalAdjusters类的nextOrSame(DayOfWeek)方法用于从任何Date对象中返回一个next-or-same DayOfWeek TemporalAdjuster对象,该对象可用于获取一个新的Date对象,该对象是next-or-same日期,并且指定了相同的DayOfWeek作为参数。将应用TempotralAdjuster。

用法:

public static TemporalAdjuster
       nextOrSame(DayOfWeek dayOfWeek)

参数:此方法接受dayOfWeek,可用于获取新的Date对象,该对象是具有相同DayOfWeek的next-or-same日期



返回值:此方法返回next-or-same DayOfWeek调节器。

以下示例程序旨在说明TemporalAdjusters.nextOrSame()方法:
程序1:

// 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

程序2:

// 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://docs.oracle.com/javase/10/docs/api/java/time/temporal/TemporalAdjusters.html#nextOrSame(java.time.DayOfWeek)




相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 TemporalAdjusters nextOrSame() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。