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


Java OffsetTime from()用法及代码示例


Java中的OffsetTime类的from()方法从时态对象获取OffsetTime的实例,该对象在方法的参数中传递。

用法:

public static OffsetTime from(TemporalAccessor temporal)

参数:此方法接受单个强制性参数temporal,该参数指定要转换的时间对象,并且不为null。


返回值:它返回偏移时间,而不是null。

以下示例程序旨在说明from()方法:

示例1:

// Java program to demonstrate the from() method 
  
import java.time.OffsetTime; 
import java.time.ZonedDateTime; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // Function called to get current time 
        OffsetTime time 
            = OffsetTime.from(ZonedDateTime.now()); 
  
        // Prints the current time 
        System.out.println("Current-time: "
                           + time); 
    } 
}
输出:
Current-time: 13:07:59.941Z

程序2

// Java program to demonstrate the from() method 
  
import java.time.OffsetTime; 
import java.time.ZonedDateTime; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // Function called to get current time 
        OffsetTime time 
            = OffsetTime.from(ZonedDateTime.now()); 
  
        // Prints the current time 
        System.out.println("Current-time: "
                           + time); 
    } 
}
输出:
Current-time: 13:08:03.087Z

参考: https://docs.oracle.com/javase/8/docs/api/java/time/OffsetTime.html#from-java.time.temporal.TemporalAccessor-



相关用法


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