package com.lty;
import java.sql.Date;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
//时间戳和Date很像,都包括年月日时分秒,两者可以互相转化
public class InstantDemo {
public static void main(String[] args) {
Instant now = Instant.now();
System.out.println(now);//当前时间戳 2022-03-31T01:59:50.793324300Z
//系统默认时间戳
ZonedDateTime dateTime = now.atZone(ZoneId.systemDefault());
System.out.println(dateTime);//2022-03-31T09:59:50.793324300+08:00[Asia/Shanghai]
java.util.Date date = Date.from(now);
System.out.println(date);//时间戳转Date Thu Mar 31 10:02:21 CST 2022
//Date转时间戳
Instant instant = date.toInstant();
System.out.println(instant);//2022-03-31T02:03:04.021Z
}
}
Instant(时间戳)的使用,得到当前时间戳,得到系统默认时间戳(Asia/Shanghai),时间戳Instant和Date的相互转换
最新推荐文章于 2024-07-15 03:36:15 发布