Instant
这个当前时间戳在java.time
中以Instant
类型表示,我们用Instant.now()
获取当前时间戳,效果和System.currentTimeMillis()
类似:
打印的结果类似:
一个是以秒为单位的时间戳,一个是更精确的纳秒精度。它和System.currentTimeMillis()
返回的相比,只是多了更高精度的纳秒。
既然Instant
就是时间戳,那么,给它附加上一个时区,就可以创建出ZonedDateTime
:
可见,对于某一个时间戳,给它关联上指定的ZoneId
,就得到了ZonedDateTime
,继而可以获得了对应时区的LocalDateTime
。
转换的时候,只需要留意long
类型以毫秒还是秒为单位即可。
Instant
表示高精度时间戳,它可以和ZonedDateTime
以及互相转换。