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)
- Java Clock class is in
java.time
package. Java Clock类在java.time
包中。 - 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是一个抽象类 ,因此我们无法实例化它。 但是,它包含几种获取其实例的静态方法 。
- 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作为参数传递给需要当前时刻的方法。
- Clock can be used instead of
System.currentTimeMillis()
andTimeZone.getDefault()
. 可以使用Clock代替System.currentTimeMillis()
和TimeZone.getDefault()
。 - Java provides four implementations of Clock –
FixedClock
,OffsetClock
,SystemClock
andTickClock
. They are part of Clock class and there are static methods that return these Clock implementations.
FixedClock
,OffsetClock
,SystemClock
和TickClock
。 它们是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.
注意TickClock
和SystemClock
实例的用法。
固定(即时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类用法的全部内容。
Reference: API Doc
参考: API文档
翻译自: https://www.journaldev.com/20781/java-clock-class-java-time-clock
java clock计时