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


Java ChronoZonedDateTime with(TemporalAdjuster)用法及代碼示例


ChronoZonedDateTime接口的with(TemporalAdjuster Adjuster)方法用於使用TemporalAdjuster調整此日期時間,並在調整後返回調整後的日期時間的副本。使用指定的調節器策略對象進行調節。此ChronoZonedDateTime實例是不可變的,不受此方法調用的影響。一個簡單的調節器用於設置一個字段,例如Year字段,而更複雜的調節器可能將時間設置為一年的最後一天。

用法:

default ChronoZonedDateTime with(TemporalAdjuster adjuster)

參數:該方法接受調節器作為要使用的調節器的參數。


返回值:此方法基於此返回ChronoZonedDateTime並進行調整。

異常:此方法引發以下異常:

  • DateTimeException–如果無法進行調整。
  • ArithmeticException–如果發生數字溢出。

以下示例程序旨在說明with()方法:

示例1:

// Java program to demonstrate 
// ChronoZonedDateTime.with() method 
  
import java.time.*; 
import java.time.chrono.*; 
import java.time.temporal.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create ChronoZonedDateTime object 
        ChronoZonedDateTime time 
            = ZonedDateTime 
                  .parse( 
                      "1918-10-25T23:12:38.543+02:00[Europe/Paris]"); 
  
        // print instance 
        System.out.println("ChronoZonedDateTime before"
                           + " adjustment: "
                           + time); 
  
        // apply with method of ChronoZonedDateTime 
        ChronoZonedDateTime updatedlocal 
            = time.with(Month.OCTOBER) 
                  .with(TemporalAdjusters 
                            .firstDayOfMonth()); 
  
        // print instance 
        System.out.println("ChronoZonedDateTime after"
                           + " adjustment: "
                           + updatedlocal); 
    } 
}
輸出:
ChronoZonedDateTime before adjustment: 1918-10-25T23:12:38.543Z[Europe/Paris]
ChronoZonedDateTime after adjustment: 1918-10-01T23:12:38.543+01:00[Europe/Paris]

示例2:

// Java program to demonstrate 
// ChronoZonedDateTime.with() method 
  
import java.time.*; 
import java.time.chrono.*; 
import java.time.temporal.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create ChronoZonedDateTime object 
        ChronoZonedDateTime time 
            = ZonedDateTime 
                  .parse( 
                      "2018-12-06T19:21:12.123+05:30[Asia/Calcutta]"); 
  
        // print instance 
        System.out.println("ChronoZonedDateTime before"
                           + " adjustment: "
                           + time); 
  
        // apply with method of ChronoZonedDateTime 
        ChronoZonedDateTime updatedlocal 
            = time.with(Month.JANUARY) 
                  .with(TemporalAdjusters 
                            .firstDayOfMonth()); 
  
        // print instance 
        System.out.println("ChronoZonedDateTime after"
                           + " adjustment: "
                           + updatedlocal); 
    } 
}
輸出:
ChronoZonedDateTime before adjustment: 2018-12-06T19:21:12.123+05:30[Asia/Calcutta]
ChronoZonedDateTime after adjustment: 2018-01-01T19:21:12.123+05:30[Asia/Calcutta]

參考: https://docs.oracle.com/javase/9/docs/api/java/time/chrono/ChronoZonedDateTime.html#with-java.time.temporal.TemporalAdjuster-



相關用法


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