Java 8 Clock getZone() 方法


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

更新日期:2019 年 7 月 30 日

143 次浏览

启动你的事业

通过完成课程获得认证

开始
广告
© . All rights reserved.