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);

更新於: 30-7 月-2019

113 次檢視

開啟您的 職業

完成課程獲得認證

開始
廣告
© . All rights reserved.