Java OffsetDateTime from()方法及示例

来源:这里教程网 时间:2026-02-17 20:59:55 作者:

Java OffsetDateTime from()方法及示例

Java中OffsetDateTime类的 from() 方法从一个时间对象中获得一个OffsetDateTime的实例。
语法 :

public static OffsetDateTime from(TemporalAccessor temporal)

参数: 该方法接受一个参数 temporal ,指定要转换的时间对象,不为空。
返回值 :它返回本地日期,不为空。
异常 :该函数抛出一个 DateTimeException ,即如果它不能转换为OffsetDateTime。
以下程序说明了from()方法:
程序1:

// Java program to demonstrate the from() method import java.time.OffsetDateTime;import java.time.ZonedDateTime; public class GFG {    public static void main(String[] args)    {         // Function used        OffsetDateTime date = OffsetDateTime.from(ZonedDateTime.now());         // Prints the date        System.out.println(date);    }}

输出

2018-12-12T08:24:12.442Z

程序2

// Java program to demonstrate the from() method import java.time.OffsetDateTime;import java.time.ZonedDateTime; public class GFG {    public static void main(String[] args)    {         // Function used        OffsetDateTime date = OffsetDateTime.from(ZonedDateTime.now());         // Prints the date        System.out.println(date);    }}

输出

2018-12-12T08:24:15.077Z

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

相关推荐