我在 java 中使用 ZonedDateTime 作为变量。
我想将变量的值(默认 UTC 时区)转换为“美国/纽约”时区,以便日期保持不变。
示例 UTC 时间上午 4:00 = 美国东部标准时间上午 12:00。从 ZonedDateTime 变量中添加或减去小时数,这样日期就不会更改。
我们如何才能实现这种转换?
最佳答案
您可以通过转换为 LocalDateTime 并返回具有指定时区的 ZonedDateTime 来实现:
ZonedDateTime zoned = ZonedDateTime.now();
LocalDateTime local = zoned.toLocalDateTime();
ZonedDateTime newZoned = ZonedDateTime.of(local, ZoneId.of("America/New_York"));
关于Java 8 将 UTC 时间转换为 EDT/EST 以便日期保持不变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52602181/