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 {@Autowiredprivate 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的四种实现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 10000Clock 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计时

java clock计时_Java Clock类– java.time.Clock相关推荐

  1. java catch自定义异常_java异常类-java自定义异常-嗨客网

    Java异常类教程 我们在 我们可以看到,在 Exception 表示的是程序中出现了问题,可以直接使用 try - catch 来捕获处理.Error 一般值 Java常见异常 类名 描述 IOEx ...

  2. java生成圆_java工具类-java实现 生成圆头像

    1.创建RoundHeadImgUtils工具类 package com.mrd.utils; import javax.imageio.ImageIO; import java.awt.*; imp ...

  3. java clock计时_Java Clock类| offset()方法与示例

    java clock计时 Clock Class offset()方法 (Clock Class offset() method) offset() method is available in ja ...

  4. java clock计时_Java Clock类| 实例的Instant()方法

    java clock计时 Clock Class Instant()方法 (Clock Class instant() method) instant() method is available in ...

  5. java clock计时_Java Clock类| systemUTC()方法与示例

    java clock计时 Clock Class systemUTC()方法 (Clock Class systemUTC() method) systemUTC() method is availa ...

  6. java clock计时_Java Clock类| systemDefaultZone()方法与示例

    java clock计时 Clock Class systemDefaultZone()方法 (Clock Class systemDefaultZone() method) systemDefaul ...

  7. java import路径_Java import以及Java类的搜索路径

    如果你希望使用Java包中的类,就必须先使用import语句导入. import语句与C语言中的 #include 有些类似,语法为: import package1[.package2-].clas ...

  8. java arrays方法_Java工具类Arrays中不得不知的常用方法

    原标题:Java工具类Arrays中不得不知的常用方法 Arrays 数组操作集数组转List ---asList 这个被"普遍"称为数组转List的方法,可能是Arrays内大家 ...

  9. java double方法_Java Double类compare()方法与示例

    java double方法 双类compare()方法 (Double class compare() method) compare() method is available in java.la ...

最新文章

  1. 大厂动态规划面试汇总,提升内功
  2. android 状态栏 背景色_技术一面:说说Android动态换肤实现原理
  3. Linux下的shell语言编程入门
  4. Oracle 性能调优 概述
  5. HDU 1757 A Simple Math Problem (矩阵快速幂)
  6. 大二暑假工作三个月后辞职,总体感悟
  7. @Async join
  8. 面试这些公司是在浪费时间
  9. Lorenz.m的Matlab,matlab
  10. VS2010工程转VS2005工程的方法
  11. python用一维数组存储学号和成绩、然后按成绩排序输出_九度oj 题目1196:成绩排序...
  12. 关于mysql出现java.sql.SQLException错误!
  13. FineReport中cjk编码转换
  14. ecos kernel 分析 转自黑嘴公 PiPi Cat
  15. 青山绿水一枝花...
  16. 经典排序算法之:堆排序
  17. 开发那点事(四)vue实现一个日历插件
  18. 从陶潜的“化”到王维的“空”
  19. Git的简单使用——连接码云
  20. [医疗信息化][DICOM教程]开篇介绍,新冠肺炎为医疗保健信息产业带来新的的紧迫性...

热门文章

  1. chromium 一些设置 --插件安装
  2. [原创]项目管理知识体系指南之 13项目干系人管理思维导图
  3. 在老ASP中使用对象的对象生存期问题
  4. [转载] Python 内置函数 lambda、filter、map、reduce
  5. [转载] Python pandas数据分析中常用方法
  6. MATLAB函数fir1
  7. Feature Extractor[VGG]
  8. 变量申明的提升,闭包,作用域,this,运算符优先级详细举例及讲解
  9. Ajax基础知识《一》
  10. 【转】Python自动化测试 (一) Eclipse+Pydev 搭建开发环境