java.time.Instant.now() // Capture the current moment in UTC.
Run Code Online (Sandbox Code Playgroud)
在内部,自纪元参考 1970-01-01T00:00:00Z(即 UTC)以来,该时刻以整秒计数加上小数秒(纳秒计数)的形式进行跟踪Z。
您的DateTime课程显然来自 Joda-Time 图书馆。该库的创建者 Stephen Colebourne 根据 JSR 310,继续用 Java 8 及更高版本中内置的java.time类替换 Joda-Time 。
Instant对于 UTC 时间,请使用Instant. 要捕获 UTC 中的当前时刻,Instant.now().
Instant表示自 UTC 1970 第一个时刻的纪元参考以来的纳秒计数。
调用System.currentTimeMillis()是相同的,自 1970 UTC 开始以来的计数,除了更粗略的毫秒分辨率而不是纳秒。实际上,传统的计算机时钟无法准确跟踪以纳秒为单位的当前时刻,因此捕获当前时刻Instant可能仅捕获微秒(通常在 Java 9 及更高版本中)或毫秒(在 Java 8 中)。
结果:无需再打电话System.currentTimeMillis()。Instant.now()代替使用。
ZonedDateTime相当于。DateTime ZonedDateTime此类代表通过特定地区(时区)的人们使用的挂钟时间看到的时刻。
ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;
Run Code Online (Sandbox Code Playgroud)

| 归档时间: |
|
| 查看次数: |
2929 次 |
| 最近记录: |