Java 8 Clock getZone() 方法

java 8programmingobject oriented programming

可以使用 Java Clock 类中的 getZone() 方法获取创建日期和时间所需的时区。此方法无需参数,并返回所需的时区。

以下程序演示了此操作 −

示例

import java.time.*;
public class Demo {
   public static void main(String[] args) {
      Clock c = Clock.systemDefaultZone();
      ZoneId z = c.getZone();
      System.out.println("The clock is: " + c);
      System.out.println("The ZoneId is: " + z);
   }
}

输出

The clock is: SystemClock[Etc/UTC]
The ZoneId is: Etc/UTC

现在让我们理解一下上面的程序。

使用 getZone() 方法获取时区,然后显示该值。演示此操作的代码片段如下 −

Clock c = Clock.systemDefaultZone();
ZoneId z = c.getZone();
System.out.println("The clock is: " + c);
System.out.println("The ZoneId is: " + z);

相关文章