Java 中 Clock system() 方法


Java 中 Clock 類的 system() 方法可用於獲取所需的 zoneId 的時鐘當前例項。此方法需要一個引數,即 zoneId 或時區,並且它返回該時區的時鐘當前例項。

演示此方法的程式如下 −

示例

import java.time.*;
public class Main {
   public static void main(String[] args) {
      ZoneId zone = ZoneId.of("Australia/Melbourne");
      Clock c = Clock.system(zone);
      ZonedDateTime zdt = c.instant().atZone(c.getZone());
      System.out.println("The Date Time for the given zone is: " + zdt.toString());
   }
}

輸出

The Date Time for the given zone is: 2019-02-06T23:03:16.395+11:00[Australia/Melbourne]

現在,讓我們來理解上述程式。

可以使用 system() 方法獲取所需的 zoneId 的時鐘當前例項。使用 toString() 方法顯示即時時間。一個演示此方法的程式碼片段如下 −

ZoneId zone = ZoneId.of("Australia/Melbourne");
Clock c = Clock.system(zone);
ZonedDateTime zdt = c.instant().atZone(c.getZone());
System.out.println("The Date Time for the given zone is: " + zdt.toString());

更新於: 30-Jul-2019

193 次瀏覽

開啟您的職業生涯

完成課程獲得認證

開始吧
廣告
© . All rights reserved.