java getsystemtime,Microsoft时区的java.time.zone.ZoneRulesProvider

I am coding against an external API that returns what appear to be windows time-zone descriptions as strings e.g. "Romance Standard Time", and I need to parse these into java ZoneIds or offsets. A list of these values is available here.

The java.time.ZoneId documentation states:

Time-zone rules are defined by governments and change frequently.

There are a number of organizations, known here as groups, that

monitor time-zone changes and collate them. The default group is the

IANA Time Zone Database (TZDB). Other organizations include IATA (the

airline industry body) and Microsoft.

So my question is, does there exist a public implementation of java.time.zone.ZoneRulesProvider from Microsoft as alluded to in the javadoc?

I'm aware that there is a relationship between the Microsoft timezones and the standard IANA TZDB, see for example this question. But what I want to know is whether there exists a standard implemenation of ZoneRulesProvider that I can pass in as a property, as described in the the javadoc:

If the system property java.time.zone.DefaultZoneRulesProvider is

defined then it is taken to be the fully-qualified name of a concrete

ZoneRulesProvider class to be loaded as the default provider, using

the system class loader.

解决方案

The Java docs are correct that IANA, IATA, and Microsoft are sources of time zone information. However, though it may be possible for a ZoneRulesProvider to be created for each, to the best of my knowledge I don't believe there are any standard ZoneRulesProvider implementations for anything other than IANA data. One could probably be written, if one was so inclined.

There is a reasonable solution though. As the other question you linked to mentioned, time zone mappings between IANA and Microsoft are maintained through the windowsZones.xml file in the Unicode CLDR. You can use this data to translate from the Microsoft time zone ID returned by the external API, to an IANA time zone name. You can use it directly, or you can take a dependency on a library that provides this functionality for you. A good library choice would be International Components for Unicode (ICU), via the ICU4J implementation found on the project page. It's getIDForWindowsID method (docs here) uses the CLDR data to provide the translation.

Alternatively, if you have any influence over the external API, chances are if it's returning Microsoft time zone IDs that it is built using .NET. If so, you could encourage the developers of that API to return a standard IANA time zone ID in their API result (perhaps as a secondary field if they are concerned about compatibility). My open source TimeZoneConverter library is appropriate for this purpose, and also uses the same CLDR data.

Note that IANA time zone IDs are already in place in several areas at Microsoft, including .NET Core on Linux/MacOS, .NET Framework via libraries such as Noda Time and others, Universal Windows Platform (UWP) applications, and a few others. The "Microsoft time zones" are an artifact of history, going back to the earliest days of Windows NT. Developers should generally be encouraged to use IANA time zone IDs, rather than Microsoft time zones, especially in external-facing APIs for interoperability with non-Windows systems. More on Microsoft time zones can be found in the timezone tag wiki.

Also - be aware that the page you linked to with the list of Microsoft time zones is almost a decade old, and is around for legacy purposes. The index columns was only ever used on Windows CE platforms, and several of those time zones no longer exist. We no longer publish a list of these time zones as a static web page, because the data changes too frequently. You can always get a list from any Windows computer via the command TZUTIL /L, or by calling TimeZoneInfo.GetSystemTimeZones() in .NET or Powershell, or by examining the registry at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones.

Full Disclosure: I help maintain the time zones at Microsoft, as well as several open source time zone libraries for .NET.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值