java clock_Java Clock system()用法及代码示例

java.time.Clock.system(ZoneId zone)方法是Clock类的静态方法,该方法返回一个时钟,该时钟使用最佳系统时钟将返回的当前时刻作为当前时钟,并将返回时钟的ZoneID设置为传递的ZoneID。如果可以使用此方法,则可以使用System.currentTimeMillis()或其他更高分辨率的时钟。

从即时转换为日期或时间时,将使用指定的时区来给出该时区的日期和时间。从此方法返回的时钟是不可变的,线程安全的和可序列化的。

用法:

public static Clock system(ZoneId zone)

参数:此方法采用强制性参数区域,该参数区域是在将即时转换为日期时间时使用的时区

返回:该方法返回给定ZoneId的Clock对象

例:

Code:

// create a Zone Id for Europe/Paris

ZoneId zoneId = ZoneId.of("Europe/Paris");

// base Clock with default zone

Clock realClock=Clock.system(zoneId);

System.out.println(clock.instant());

Output::

2018-08-21T10:25:52.361Z

Explanation::

when you call system(ZoneId) for Clock then the system(ZoneId)

method will return a Class Object for the given ZoneId.you can get

date and time of clock by using instant of class.

以下示例程序旨在说明java.time.Clock类的system(ZoneId)方法:

程序1:当使用system(ZoneId)创建Clock时,其中ZoneId为“Europe/Paris”并打印时钟的日期和时间。

// Java program to demonstrate

// system(ZoneId) method of Clock class

import java.time.*;

// create class

public class systemMethodDemo {

// Main method

public static void main(String[] args)

{

// create a Zone Id for Europe/Paris

ZoneId zoneId = ZoneId.of("Europe/Paris");

// create Clock with system(zoneId) method

Clock clock = Clock.system(zoneId);

// get instant of class

Instant instant = clock.instant();

// get ZonedDateTime object from instantObj to get date time

ZonedDateTime time = instant.atZone(clock.getZone());

// print details of time

System.out.println("Instant for class is " + time.toString());

}

}

输出:

Instant for class is 2018-08-22T13:53:35.779+02:00[Europe/Paris]

程序2:使用system()创建带有区域“US/Arizona”的时钟,并使用getZone()打印zoneId。

// Java program to demonstrate

// system(ZoneId) method of Clock class

import java.time.*;

// create class

public class systemMethodDemo {

// Main method

public static void main(String[] args)

{

// create a Zone Id for US/Arizona

ZoneId zoneId = ZoneId.of("US/Arizona");

// create Clock with system(zoneId) method

Clock clock = Clock.system(zoneId);

// print details of ZoneId of new Clock

System.out.println("ZoneID of class is "

+ clock.getZone());

}

}

输出:

ZoneID of class is US/Arizona

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值