Java中的Clock systemDefaultZone()方法

可以使用systemDefaultZone()Java中Clock类中的方法获得具有默认时区的当前时钟实例。此方法不需要任何参数,它会返回带有默认时区的当前时钟实例。

演示此的程序如下所示-

示例

import java.time.*;
public class Demo {
   public static void main(String[] args) {
      Clock c = Clock.systemDefaultZone();
      Instant i = c.instant();
      ZonedDateTime zdt = i.atZone(c.getZone());
      System.out.println(zdt.toString());
   }
}

输出结果

2019-02-07T07:55:24.162Z[Etc/UTC]

现在让我们了解上面的程序。

使用方法获得具有默认时区的当前时钟实例systemDefaultZone()。然后使用方法打印ZonedDateTime toString()。演示这的代码片段如下-

Clock c = Clock.systemDefaultZone();
Instant i = c.instant();
ZonedDateTime zdt = i.atZone(c.getZone());
System.out.println(zdt.toString());