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


Java Instant adjustInto()用法及代码示例


Instant类的AdjustInto(Temporal时间)方法将传递的时间对象调整为具有应用此方法的此瞬间。

用法:

public Temporal adjustInto(Temporal temporal)

参数:该方法接受作为要调整的目标时间对象的参数时间。它不能为空。


返回值:此方法返回调整后的时间对象。

异常:此方法引发以下异常:

  • DateTimeException如果此方法无法进行调整。
  • ArithmeticException如果调整时发生数字溢出。

以下示例程序旨在说明Instant.adjustInto()方法:

示例1:

// Java program to demonstrate 
// Instant.adjustInto() method 
  
import java.time.*; 
import java.time.temporal.Temporal; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // create an instance object 
        Instant instant 
            = Instant.parse("2018-11-20T16:55:30.00Z"); 
  
        // create a Temporal object 
        // which is equal to OffsetDateTime object 
        OffsetDateTime passTemporal 
            = OffsetDateTime.now(); 
  
        // print passed Value 
        System.out.println("Passed Value: "
                           + passTemporal); 
  
        // apply adjustInto method 
        // to adjust OffsetDateTime Temporal 
        // to instant object 
        Temporal returnTemporal 
            = instant.adjustInto(passTemporal); 
  
        // print results 
        System.out.println("Returned Value: "
                           + (OffsetDateTime)returnTemporal); 
    } 
}
输出:
Passed Value: 2018-11-22T09:22:17.297Z
Returned Value: 2018-11-20T16:55:30Z

示例2:

// Java program to demonstrate 
// Instant.adjustInto() method 
  
import java.time.*; 
import java.time.temporal.Temporal; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create an instance object 
        Instant instant 
            = Instant.parse("2018-11-17T06:50:39.00Z"); 
  
        // create a Temporal object 
        // which is equal to ZonedDateTime object 
        ZonedDateTime passTemporal 
            = ZonedDateTime.now(); 
  
        // print passed Value 
        System.out.println("Passed Value: "
                           + passTemporal); 
  
        // apply adjustInto method 
        // to adjust ZonedDateTime Temporal 
        // to instant object 
        Temporal returnTemporal 
            = instant.adjustInto(passTemporal); 
  
        // print results 
        System.out.println("Returned Value: "
                           + (ZonedDateTime)returnTemporal); 
    } 
}
输出:
Passed Value: 2018-11-22T09:22:20.995Z[Etc/UTC]
Returned Value: 2018-11-17T06:50:39Z[Etc/UTC]

示例3:

// Java program to demonstrate 
// Instant.adjustInto() method 
  
import java.time.*; 
import java.time.temporal.Temporal; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create an instance object 
        Instant instant 
            = Instant.parse("2017-11-01T16:25:00.00Z"); 
  
        // create a Temporal object 
        // which is equal to Instant object 
        // with current Instant 
        Temporal passTemporal 
            = Instant.now(); 
  
        // print passed Value 
        System.out.println("Passed Value: "
                           + passTemporal); 
  
        // apply adjustInto method to adjust Temporal 
        // to this instant object 
        Temporal returnTemporal 
            = instant.adjustInto(passTemporal); 
  
        // print results 
        System.out.println("Returned Value: "
                           + returnTemporal); 
    } 
}
输出:
Passed Value: 2018-11-22T09:22:23.298Z
Returned Value: 2017-11-01T16:25:00Z

参考:https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#adjustInto(java.time.temporal.Temporal)



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 Instant adjustInto() method in Java with Example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。