Java offsetTime from()方法及示例

来源:这里教程网 时间:2026-02-17 21:00:24 作者:

Java offsetTime from()方法及示例

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

语法

public static OffsetTime from(TemporalAccessor temporal)

参数: 该方法接受一个强制参数 temporal ,指定要转换的时间对象,且不为空。

返回值: 它返回偏移的时间,并且不为空。

下面的程序说明了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-

相关推荐