LocalDateTime转化为UTC
时间: 2024-05-14 07:10:40 浏览: 350
要将LocalDateTime转化为UTC,首先需要将LocalDateTime转化为Instant,然后再通过Instant对象的atZone()方法将其转化为ZonedDateTime对象,最后再通过ZonedDateTime对象的toInstant()方法将其转化为Instant对象并获取其时间戳即可。
以下是Java代码示例:
```
// 获取当前时间的LocalDateTime对象
LocalDateTime localDateTime = LocalDateTime.now();
// 将LocalDateTime对象转化为Instant对象
Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
// 将Instant对象转化为ZonedDateTime对象
ZonedDateTime zonedDateTime = instant.atZone(ZoneId.of("UTC"));
// 获取ZonedDateTime对象对应的Instant对象的时间戳
long timestamp = zonedDateTime.toInstant().toEpochMilli();
```
阅读全文
相关推荐



