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


Java ZoneOffset adjustInto(Temporal)用法及代碼示例


java.time包中ZoneOffset類的AdjustInto(Temporal)方法用於獲取調整後的Temporal實例,並將該ZoneOffset實例調整為該實例。此方法將Temporal實例作為參數,並返回Temporal實例,它是調整後的實例。

用法:

public Temporal adjustInto(Temporal temporalInstance)

參數:此方法接受接受參數temporalInstance,該參數是要調整的Temporal實例。


返回值:此方法返回一個Temporal實例,該實例是此Temporal實例的調整後的值。

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

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

以下示例說明了ZoneOffset.adjustInto()方法:

示例1:

// Java code to illustrate adjustInto() method 
  
import java.time.temporal.*; 
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // Get the Temporal instance 
        ZonedDateTime temporalInstance 
            = ZonedDateTime.now(); 
  
        System.out.println("Original Temporal instance: "
                           + temporalInstance); 
  
        // Get the ZoneOffset 
        ZoneOffset zoneOffset 
            = ZoneOffset.ofHours(5); 
  
        // Using adjustInto() method 
        ZonedDateTime adjustedTemporal 
            = (ZonedDateTime)zoneOffset 
                  .adjustInto(temporalInstance); 
  
        System.out.println("Adjusted Temporal instance: "
                           + adjustedTemporal); 
    } 
}
輸出:
Original Temporal instance: 2018-12-11T09:44:14.373Z[Etc/UTC]
Adjusted Temporal instance: 2018-12-11T09:44:14.373Z[Etc/UTC]

示例2:

// Java code to illustrate adjustInto() method 
  
import java.time.temporal.*; 
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // Get the Temporal instance 
        OffsetDateTime temporalInstance 
            = OffsetDateTime.now(); 
  
        System.out.println("Original Temporal instance: "
                           + temporalInstance); 
  
        // Get the ZoneOffset 
        ZoneOffset zoneOffset 
            = ZoneOffset.ofHours(5); 
  
        // Using adjustInto() method 
        Temporal adjustedTemporal 
            = zoneOffset 
                  .adjustInto(temporalInstance); 
  
        System.out.println("Adjusted Temporal instance: "
                           + adjustedTemporal); 
    } 
}
輸出:
Original Temporal instance: 2018-12-11T09:44:16.893Z
Adjusted Temporal instance: 2018-12-11T09:44:16.893+05:00

參考: Oracle Doc



相關用法


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