LocalDateTime转时间毫秒值
·
可以先转为Timestamp或时间戳
具体如下
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
System.out.println(System.currentTimeMillis()); //当前系统时刻(亚洲/上海)1670130498855
//LocalDateTime 转时间毫秒值
//方式一
long time = Timestamp.valueOf(now).getTime();
System.out.println(time); //与+8是和当前系统时刻相等(亚洲/上海)1670130498855
//方式二
long time8 = now.toInstant(ZoneOffset.of("+8")).toEpochMilli();
System.out.println(time8); //与当前系统时刻相等(亚洲/上海)1670130498855
//其他注意
LocalDateTime now1 = LocalDateTime.now(ZoneOffset.UTC);
long time0 = now1.toInstant(ZoneOffset.of("+0")).toEpochMilli();
long timeUtc = now1.toInstant(ZoneOffset.UTC).toEpochMilli();
System.out.println(time0); //1670130498855
System.out.println(timeUtc); //1670130498855
}
更多推荐
已为社区贡献1条内容
所有评论(0)