Instant atZone() 方法在 Java 中
可以通过使用 Java 中 Instant 类中的 atZone() 方法将 Instant 与时区合并,以创建一个 ZonedDateTime 对象。该方法需要一个参数,即 ZoneID,并返回 ZonedDateTime 对象。
演示这一点的程序如下
示例
import java.time.*;
public class Demo {
public static void main(String[] args) {
Instant i = Instant.parse("2019-01-13T18:35:19.00Z");
System.out.println("The Instant object is: " + i);
ZonedDateTime zdt = i.atZone(ZoneId.of("Australia/Melbourne"));
System.out.println("The ZonedDateTime object is: " + zdt);
}
}输出
The Instant object is: 2019-01-13T18:35:19Z The ZonedDateTime object is: 2019-01-14T05:35:19+11:00[Australia/Melbourne]
现在让我们来理解一下上面的程序。
首先,显示当前时间。然后,Instant 与时区结合,以使用 atZone() 方法创建一个 ZonedDateTime 对象。将显示 ZonedDateTime 对象。展示这一点的代码段如下
Instant i = Instant.parse("2019-01-13T18:35:19.00Z");
System.out.println("The Instant object is: " + i);
ZonedDateTime zdt = i.atZone(ZoneId.of("Australia/Melbourne"));
System.out.println("The ZonedDateTime object is: " + zdt);
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP