public static void main(String[] args){
LocalDateTime now = LocalDateTime.now();
LocalDateTime localDateTime = changeTimeZone(now, "GMT+8", "GMT-7");
System.out.println(localDateTime.toString());
}
/**
*
* @param time 需要转换的时间
* @param fromZone 需要转换的时间的时区,即time的时区
* @param toZone 需要转成的时区
* @return 转换时区之后的时间
*/
public static LocalDateTime changeTimeZone(LocalDateTime time, String fromZone, String toZone) {
ZonedDateTime zonedtime = time.atZone(ZoneId.of(fromZone));
ZonedDateTime converted = zonedtime.withZoneSameInstant(ZoneId.of(toZone));
return converted.toLocalDateTime();
}
java实现时区转换
最新推荐文章于 2024-07-28 16:11:54 发布