Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to translate between windows and IANA timezones in java

I need to translate between IANA timezone & windows timezone & vice-versa. There is another question reported: How to translate between Windows and IANA time zones?

It specifies that Noda time library can be used in .Net

Do we have any library to be used in Java? Or any other utility to be used in java?

like image 516
Saurabhcdt Avatar asked Jul 16 '15 09:07

Saurabhcdt


People also ask

How do I convert between time zones in Java?

To convert any time to the specific timezone (for example: UTC -> local timezone and vise versa) with any time pattern you can use java. time library. This method will take time patterns (original and required format) and timezone (original time zone and required timezone) will give String as output.

What are the Java TimeZone ids?

ECT - Europe/Paris. IET - America/Indiana/Indianapolis. IST - Asia/Kolkata. JST - Asia/Tokyo.

How do I change TimeZone in python?

Use the datetime. astimezone() method to convert the datetime from one timezone to another. This method uses an instance of the datetime object and returns a new datetime of a given timezone.

How to convert IANA time zone to Windows Time Zone?

If you run the program with a single command line parameter, then we treat it as an IANA time zone and ask ucal_get­Windows­Time­Zone­ID to convert it to a Windows time zone.

What is the IANA time zone in the tzdb?

Those provided by IANA in the TZDB are identified by a value such as America/New_York. Many Internet-based APIs use the IANA time zones, but for numerous reasons one might need to convert this to a Windows time zone id, or vice-versa.

What time zones should I use for Windows globalization?

Bonus chatter: The Windows globalization team also strongly recommends that programs use IANA time zones and use the Windows registry-based time zones only for legacy interop purposes.

Does JavaScript know about time zones?

Sadly the browsers or better said EcmaScript (a.k.a JavaScript™) for long time had no idea of time zones at all. Even more said is that windows knows about time zones but does not care at all about the IANA ones.


2 Answers

This may be what you need, but I don't know if it will work for all your use cases:

for (String tzId : TimeZone.getAvailableIDs()) {
  TimeZone tz = TimeZone.getTimeZone(tzId);
  if (tz.getDisplayName(Locale.ENGLISH).equals("Eastern Standard Time")) {
    System.out.println("tz = " + tzId);
  }
}
like image 95
assylias Avatar answered Sep 19 '22 20:09

assylias


I have implemented support for Windows zones in my Java-library Time4J. The last version v4.2 is also interoperable with Java-8 so it is easy to convert all basic Time4J-types to java.time-equivalents. For example recognizing Windows zones as strings is possible in constructing as well as during parsing:

  // 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

As you can see, a locale Information is necessary to map a Windows zone like "Eastern Standard Time" to an Olson/IANA-identifier like "America/New_York". The underlying data and mapping informations are taken from CLDR.

The reverse way from IANA to Windows might be done this simple way:

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

However, this reverse conversion might not work for all iana-identifiers because Windows only supports a very simplified subset of timezones compared with IANA-TZDB. I also think that the reverse way is hardly used in practice. Users should rather work with IANA-timezones by default and only use Windows timezones if that is the (unavoidable) input to handle (see first part of my answer).

like image 23
Meno Hochschild Avatar answered Sep 17 '22 20:09

Meno Hochschild