java clock计时_Java Clock类– java.time.Clock

java clock计时

Java Clock class is part of Date Time API. Java Clock class is used to get the current instance, date and time with the time zone.

Java Clock类是Date Time API的一部分。 Java Clock类用于获取带有时区的当前实例,日期和时间。

Java Clock类 (Java Clock Class)

  1. Java Clock class is in java.time package.

    Java Clock类在java.time包中。
  2. Java Clock is an abstract class, so we can’t instantiate it. However it contains several static methods to get it’s instance.

    Java Clock是一个抽象类 ,因此我们无法实例化它。 但是,它包含几种获取其实例的静态方法
  3. Java Clock class usage is optional, because most of the Date API classes has now() method. It’s main purpose is to allow alternate clocks to be plugged in as and when required. For example in Spring Dependency Injection.
    public class MySpringBean {
        @Autowired
        private Clock clock; 
    }

    Best practice is to pass a Clock as argument into method that requires the current instant.

    now()方法。 它的主要目的是允许在需要时插入备用时钟。 例如在Spring Dependency Injection中

    最佳做法是将Clock作为参数传递给需要当前时刻的方法。

  4. Clock can be used instead of System.currentTimeMillis() and TimeZone.getDefault().

    可以使用Clock代替System.currentTimeMillis()TimeZone.getDefault()
  5. Java provides four implementations of Clock – FixedClock, OffsetClock, SystemClock and TickClock. They are part of Clock class and there are static methods that return these Clock implementations.
    Java Clock Class implementations

    Java提供Clock的四种实现FixedClockOffsetClockSystemClockTickClock 。 它们是Clock类的一部分,并且有静态方法返回这些Clock实现。

Java Clock方法 (Java Clock Methods)

Let’s look into Clock class static methods and their usage.

让我们看一下Clock类的静态方法及其用法。

systemDefaultZone() (systemDefaultZone())

Clock systemDefaultZone() method returns the Clock instance with system default time zone.

Clock systemDefaultZone()方法返回带有系统默认时区的Clock实例。

Clock clock = Clock.systemDefaultZone();
System.out.println(clock.getZone()); // prints "Asia/Kolkata" for me

瞬间() (instant())

This method returns the current instant of the clock.

此方法返回时钟的当前时刻。

Instant instant = clock.instant();
System.out.println(instant); //prints "2018-05-08T17:51:09.102302Z"

systemUTC() (systemUTC())

This method returns the Clock instance with UTC time zone.

此方法返回带有UTC时区的Clock实例。

clock = Clock.systemUTC();
System.out.println(clock.getZone()); //prints "Z"

系统(ZoneId区域) (system(ZoneId zone))

This method is used to get the Clock instance with specified time zone.

此方法用于获取具有指定时区的Clock实例。

Clock clock = Clock.system(ZoneId.of("Europe/Paris"));
System.out.println(clock.instant()); // prints "2018-05-08T17:51:09.116133Z"

millis() (millis())

This method returns the current milliseconds of the instance. It’s equivalent to System.currentTimeMillis().

此方法返回实例的当前毫秒数。 它等效于System.currentTimeMillis()

System.out.println(clock.millis()); //prints 1525801869116
System.out.println(System.currentTimeMillis()); // prints 1525801869116

offset(Clock baseClock,持续时间offsetDuration) (offset(Clock baseClock, Duration offsetDuration))

This method is used to get a Clock with instance added to the given base clock. We can used it to simulate future and past time testing.

此方法用于获取将实例添加到给定基本时钟的Clock。 我们可以使用它来模拟未来和过去的时间测试。

Clock pastClock = Clock.offset(clock, Duration.ofMillis(-10000));
System.out.println(clock.millis() - pastClock.millis()); //prints 10000

Clock futureClock = Clock.offset(clock, Duration.ofDays(1));
System.out.println(futureClock.millis() - clock.millis()); //prints 86400000

tick(时钟baseClock,持续时间tickDuration) (tick(Clock baseClock, Duration tickDuration))

This method returns Clock that returns instants from the specified base clock truncated to the nearest occurrence of the specified duration. Let’s see it’s usage with an example code snippet.

此方法返回Clock,该Clock返回从指定的基本时钟截断到指定持续时间的最接近值的瞬间。 让我们用示例代码片段来查看它的用法。

Clock nearestHourClock = Clock.tick(clock, Duration.ofHours(1));
System.out.println(clock.instant());
System.out.println(nearestHourClock.instant());
		
Clock nearestSecondClock = Clock.tickSeconds(ZoneId.systemDefault());
System.out.println(nearestSecondClock);
System.out.println(nearestSecondClock.instant());

Output of above code snippet:

上面的代码片段的输出:

2018-05-08T17:51:09.116566Z
2018-05-08T17:00:00Z
TickClock[SystemClock[Asia/Kolkata],PT1S]
2018-05-08T17:51:09Z

Notice the usage of TickClock and SystemClock instances.

注意TickClockSystemClock实例的用法。

固定(即时fixedInstant,ZoneId区域) (fixed(Instant fixedInstant, ZoneId zone))

This method returns a Clock that always returns the same instant.

此方法返回一个Clock,该Clock始终返回同一时刻。

Clock fixedClock = Clock.fixed(instant, ZoneId.systemDefault());
System.out.println(fixedClock);
System.out.println(fixedClock.instant());
Thread.sleep(1000);
System.out.println(fixedClock.instant());

Output:

输出:

FixedClock[2018-05-08T17:51:09.102302Z,Asia/Kolkata]
2018-05-08T17:51:09.102302Z
2018-05-08T17:51:09.102302Z

That’s all for Java Clock class usage with examples.

这就是示例中Java Clock类用法的全部内容。

GitHub Repository. GitHub Repository下载完整的代码。

Reference: API Doc

参考: API文档

翻译自: https://www.journaldev.com/20781/java-clock-class-java-time-clock

java clock计时

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值