當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java TemporalAdjusters next()用法及代碼示例

TemporalAdjusters類的next(DayOfWeek)方法用於返回下一個day-of-week TemporalAdjuster對象,該對象可用於獲取新的Date對象,該對象是具有相同匹配DayOfWeek的下一個日期,該日期作為參數從任何Date對象上傳遞將應用此TempotralAdjuster。

用法:

public static TemporalAdjuster next(DayOfWeek dayOfWeek)

參數:此方法接受dayOfWeek,可用於獲取新的Date對象,該對象是具有相同匹配的DayOfWeek的下一個日期。



返回值:此方法返回星期幾的第二天,而不是null。

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

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

程序2:

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




相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 TemporalAdjusters next() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。