简介
- 我们已经讲过,计算机存储的当前时间,本质上只是一个不断递增的整数。Java提供的System.currentTimeMillis()返回的就是以毫秒表示的当前时间戳。
- 这个当前时间戳在java.time中以Instant类型表示,我们用Instant.now()获取当前时间戳,效果和System.currentTimeMillis()类似
```java
public class InstantTest {
public static void main(String[] args) {
} }Instant now = Instant.now();System.out.println(now.getEpochSecond()); // 秒System.out.println(now.toEpochMilli()); // 毫秒
- Instant表示高精度时间戳,它可以和ZonedDateTime以及long互相转换。
```java
public class InstantTest {
public static void main(String[] args) {
Instant now = Instant.now();
System.out.println(now.getEpochSecond()); // 秒
System.out.println(now.toEpochMilli()); // 毫秒
// 以指定时间戳创建Instant:
Instant ins = Instant.ofEpochSecond(1568568760);
ZonedDateTime zdt = ins.atZone(ZoneId.systemDefault());
System.out.println(zdt);
}
}
