访问量: 9 次浏览
Java中 ZonedDateTime 类的 from() 方法是用来从作为参数传递的 TemporalAccessor 对象中获取 ZonedDateTime 的实例。一个 TemporalAccessor 代表了一组任意的日期和时间信息,这个方法有助于根据指定的 TemporalAccessor 对象获得 ZonedDateTime 的一个瞬间。
public static ZonedDateTime
from(TemporalAccessor temporal)
参数:该方法接受一个参数 temporal ,代表要转换的时间对象。这是一个强制参数,它不应该是空的。
返回值:该方法返回一个分区的日期时间。
异常:如果不能转换为一个分区日期时间,该方法会抛出一个 DateTimeException 。
下面的程序说明了 from() 方法:
程序1:
// Java program to demonstrate
// ZonedDateTime.from() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create a ZonedDateTime object
ZonedDateTime zonedDT
= ZonedDateTime.now();
// create a ZonedDateTime object using
// from() method
ZonedDateTime result = ZonedDateTime.from(zonedDT);
// print result
System.out.println("ZonedDateTime: "
+ result);
}
}
输出。
ZonedDateTime: 2018-12-12T19:03:06.445Z[Etc/UTC]
程序2
// Java program to demonstrate
// ZonedDateTime.from() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create a OffsetDateTime object
OffsetDateTime offset
= OffsetDateTime.now();
// create a ZonedDateTime object using
// from() method
ZonedDateTime result = ZonedDateTime.from(offset);
// print result
System.out.println("ZonedDateTime: "
+ result);
}
}
输出。
ZonedDateTime: 2018-12-12T19:03:09.523Z