public static LocalDateTime convertToShanghaiTime(LocalDateTime utcLocalDateTime) {
if(ObjectUtils.isEmpty(utcLocalDateTime)){
return null;
}
// 创建表示零时区(UTC)的ZoneId
ZoneId utcZoneId = ZoneId.of("UTC");
// 将LocalDateTime与UTC时区结合,创建一个ZonedDateTime对象
ZonedDateTime utcZonedDateTime = utcLocalDateTime.atZone(utcZoneId);
// 获取东八区的ZoneId
ZoneId shanghaiZoneId = ZoneId.of("Asia/Shanghai");
// 使用withZoneSameInstant方法将UTC时间的ZonedDateTime转换为东八区时间的ZonedDateTime
// 注意:withZoneSameInstant会保持瞬间时间(毫秒数)不变,仅转换时区
ZonedDateTime shanghaiZonedDateTime =
utcZonedDateTime.withZoneSameInstant(shanghaiZoneId);
return shanghaiZonedDateTime.toLocalDateTime();
}
12-21
1万+