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