首页 > 解决方案 > 如何通过UTC偏移量获取时区?

问题描述

我有 UTC 偏移量 -05:00,我想通过偏移量获取时区名称。

ZoneOffset offset = ZoneOffset.of("-05:00");
System.out.println(offset.getId()); //prints -05:00

我想得到这样的结果:

America/Chicago
America/Eirunepe
Etc/GMT+5
Mexico/General 
...

我想要现在偏移的时区-05:00(而不是那些可能在一年中的另一个时间的时区)。

标签: javatimezone-offset

解决方案


是的,你可以做到,看看这个。

final List<ZoneId> timeZoneByUtc = ZoneId.getAvailableZoneIds().stream().map(ZoneId::of)
        .filter(z -> z.getRules().getOffset(Instant.now()).equals(ZoneOffset.ofHours(-5)))
        .collect(Collectors.toList());

推荐阅读