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


Java ChronoZonedDateTime with(TemporalField, long)用法及代碼示例

ChronoZonedDateTime接口的with(TemporalField字段,long newValue)方法用於將ChronoZonedDateTime的指定字段設置為新值,並返回新時間的副本。此方法可用於更改任何受支持的字段,例如年,日,月,小時,分鍾或秒。如果由於不支持該字段或其他原因而無法設置新值,則會引發異常。此ChronoZonedDateTime實例是不可變的,不受此方法調用的影響。

用法:

ChronoZonedDateTime with(TemporalField field, 
                         long newValue)

參數:此方法接受兩個參數:


  • field這是要在結果中設置的字段,
  • newValue其中結果中字段的新值作為參數。

返回值:此方法基於此方法並帶有指定的字段集,返回ChronoZonedDateTime。

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

  • DateTimeException-如果無法進行調整。
  • UnsupportedTemporalTypeException-如果不支持該字段。
  • 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( 
                      "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(ChronoField.YEAR, 2017); 
  
        // 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:2017-12-06T19:21:12.123+05:30[Asia/Calcutta]

程序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( 
                      "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(ChronoField.MONTH_OF_YEAR, 1); 
  
        // print instance 
        System.out.println("ChronoZonedDateTime after"
                           + " adjustment:"
                           + updatedlocal); 
    } 
}
輸出:
ChronoZonedDateTime before adjustment:1918-10-25T23:12:38.543Z[Europe/Paris]
ChronoZonedDateTime after adjustment:1918-01-25T23:12:38.543Z[Europe/Paris]

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



相關用法


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