📌  相关文章
📜  Java中的 OffsetDateTime from() 方法及示例

📅  最后修改于: 2022-05-13 01:54:48.437000             🧑  作者: Mango

Java中的 OffsetDateTime from() 方法及示例

Java中 OffsetDateTime 类的from()方法从时间对象中获取 OffsetDateTime 的实例。
句法 :

public static OffsetDateTime from(TemporalAccessor temporal)

参数:此方法接受单个参数temporal ,它指定要转换的时间对象,而不是 null。
返回值:返回本地日期,不为空。
异常:如果函数无法转换为 OffsetDateTime,则该函数会引发DateTimeException
下面的程序说明了from()方法:
程序 1:

Java
// 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);
    }
}


Java
// 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

方案二

Java

// 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: Java/time/OffsetDateTime.html#from-java.time.temporal.TemporalAccessor-