
计算机存储的当前时间,本质上只是一个不断递增的整数。Java提供的System.currentTimeMillis()
返回的就是以毫秒表示的当前时间戳。
这个当前时间戳在java.time
中以Instant
类型表示,我们用Instant.now()
获取当前时间戳,效果和System.currentTimeMillis()
类似:
public class Main { public static void main(String[] args) { Instant now = Instant.now(); System.out.println(now.getEpochSecond()); // 秒 1568568760 System.out.println(now.toEpochMilli()); // 毫秒 1568568760316 } }
实际上,Instant
内部只有两个核心字段:
public final class Instant implements ... { private final long seconds; private final int nanos; }
一个是以秒为单位的时间戳,一个是更精确的纳秒精度。它和System.currentTimeMillis()
返回的long
相比,只是多了更高精度的纳秒。
既然Instant
就是时间戳,那么,给它附加上一个时区,就可以创建出ZonedDateTime
:
// 以指定时间戳创建Instant: Instant ins = Instant.ofEpochSecond(1568568760); ZonedDateTime zdt = ins.atZone(ZoneId.systemDefault()); System.out.println(zdt); // 2019-09-16T01:32:40+08:00[Asia/Shanghai]
可见,对于某一个时间戳,给它关联上指定的ZoneId
,就得到了ZonedDateTime
,继而可以获得了对应时区的LocalDateTime
。
所以,LocalDateTime
,ZoneId
,Instant
,ZonedDateTime
和long
都可以互相转换:
┌─────────────┐ │LocalDateTime│────┐ └─────────────┘ │ ┌─────────────┐ ├───>│ZonedDateTime│ ┌─────────────┐ │ └─────────────┘ │ ZoneId │────┘ ▲ └─────────────┘ ┌─────────┴─────────┐ │ │ ▼ ▼ ┌─────────────┐ ┌─────────────┐ │ Instant │<───>│ long │ └─────────────┘ └─────────────┘
转换的时候,只需要留意long
类型以毫秒还是秒为单位即可。
小结
Instant
表示高精度时间戳,它可以和ZonedDateTime
以及long
互相转换。
热门文章
- VPN Tiktok | 3月18日21.6M/S|免费VPN/Shadowrocket/SSR/V2ray/Clash免费节点订阅分享
- VPN Tiktok | 4月10日22.8M/S|免费VPN/Shadowrocket/Clash/V2ray/SSR免费节点订阅分享
- Go 互斥锁(sync.Mutex)和 读写锁(sync.RWMutex)
- vue实现文字滚动效果_vue.js
- 宠物领养网站设计参考文献有哪些呢(宠物领养网站设计参考文献有哪些呢)
- 南京宠物狗领养地址查询电话(南京狗狗领养中心在哪里)
- 宠物店粮食摆放图片大全(宠物店的粮适合买吗)
- VPN Tiktok | 4月11日21M/S|免费VPN/Clash/V2ray/SSR/Shadowrocket免费节点订阅分享
- 小猫养多久才认家(小猫刚开始养多久能认识主人)
- VPN Tiktok | 4月15日19.2M/S|免费VPN/SSR/Shadowrocket/Clash/V2ray免费节点订阅分享