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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。