如何在 windows 和 java 中的 IANA 时区之间转换

How to translate between windows and IANA timezones in java

我需要在 IANA 时区和 windows 时区之间进行转换,反之亦然。还有一个问题反馈:How to translate between Windows and IANA time zones?

指定Noda时间库可以在.Net中使用

Java我们有任何库可以使用吗?或者在 java?

中使用的任何其他实用程序

这可能是您所需要的,但我不知道它是否适用于您的所有用例:

for (String tzId : TimeZone.getAvailableIDs()) {
  TimeZone tz = TimeZone.getTimeZone(tzId);
  if (tz.getDisplayName(Locale.ENGLISH).equals("Eastern Standard Time")) {
    System.out.println("tz = " + tzId);
  }
}

我已经实现了 support for Windows zones in my Java-library Time4J. The last version v4.2 is also interoperable with Java-8,因此很容易将所有基本的 Time4J 类型转换为 java.time 等价物。例如,在构造和解析过程中可以将 Windows 区域识别为字符串:

  // conversion Windows to IANA
  WindowsZone wzn = WindowsZone.of("Eastern Standard Time");
  TZID winzone = wzn.resolveSmart(Locale.US);
  System.out.println(winzone.canonical()); // WINDOWS~America/New_York

  // usage in timezone calculation
  Timezone tz = Timezone.of(winzone);
  System.out.println(Moment.UNIX_EPOCH.toZonalTimestamp(winzone)); // 1969-12-31T19

  // usage in parsing and formatting
  ChronoFormatter<Moment> f =
    ChronoFormatter.ofMomentPattern(
      "MM/dd/uuuu hh:mm a zzzz", PatternType.CLDR, Locale.US, winzone);
  Moment pacificTime = f.parse("07/17/2015 02:45 PM Pacific Standard Time");
  System.out.println(f.format(pacificTime)); // 07/17/2015 05:45 PM Eastern Standard Time

如您所见,需要语言环境信息才能将 "Eastern Standard Time" 等 Windows 区域映射到 "America/New_York" 等 Olson/IANA-identifier 区域。底层数据和映射信息取自CLDR。

从 IANA 到 Windows 的反向方法可以通过这种简单的方式完成:

String iana = "America/New_York";
String winzone = "WINDOWS~" + iana;
NameStyle dummy = NameStyle.LONG_STANDARD_TIME; // does not really matter
String name = Timezone.of(winzone).getDisplayName(dummy, Locale.US);
System.out.println(name); // Eastern Standard Time

但是,此反向转换可能不适用于所有 iana 标识符,因为与 IANA-TZDB 相比,Windows 仅支持非常简化的时区子集。我也认为在实践中几乎没有使用反向方式。用户应该默认使用 IANA 时区,如果这是(不可避免的)要处理的输入,则只使用 Windows 时区(请参阅我的回答的第一部分)。

我终于不得不自己实现了。 windowsZones.xml 需要更新大量缺失的时区条目。我不会发布更新后的文件,因为有许多时区 Windows 偏移量和 IANA 偏移量之间没有完美匹配。

另外,一个 Windows 时区可能有多个 IANA 时区。因此,我必须根据其他可用信息(如用户的地理位置(地址)等)来选择最合适的实施方式

有了这个,我只是使用 windowsZones.xml 从 Windows 时区获取 IANA 时区,反之亦然。