3

I am implementing a setting where the user selects a timezone from a dropdown list, then my app displays the current time in the selected timezone using moment-timezone.js.

I am using TimeZone.getAvailableIDs() to retrieve the list of timezone from the server side to create the dropdown. However, moment-timezone.js is unable to parse some timezone IDs from the list. I checked the size of the list and compared it with moment.tz.names() from moment-timezone.js, and found that the list from moment-timezone.js has about 30 less IDs than the list from Java. I suspect that it may have something to do with the versions of the timezones but I am not sure, as I am using Java 7 and the version I found in ZoneInfoMappings is 2014b, while the data file I have for moment-timezone.js is 2016f.

Is there any way to make both Java and moment-timezone.js retrieve data from the same list so that I can have them synchronized? I am limited to Java 7 and cannot use Time from Java 8, and I prefer to use native Java libraries so I am not considering Joda Time for now.

UserF40
  • 3,533
  • 2
  • 23
  • 34
lukforce
  • 33
  • 4
  • Hi @lukforce, to improve your question you should post the code you have tried as it will lead to better answers. – David Oct 10 '16 at 19:19

2 Answers2

1

A few things:

  • Both Java and Moment-Timezone use the same source data, the tz database from IANA. As revisions to this data are released multiple times per year, you should be sure to stay updated, and use the same version on both sides.

  • Use the TZUpdater utility to update the time zone data in Java.

  • Be aware that there are a handful of additional three-letter identifiers supported by Java for legacy purposes that are not TZDB identifiers. From the JavaDoc on TimeZone:

    Three-letter time zone IDs

    For compatibility with JDK 1.1.x, some other three-letter time zone IDs (such as "PST", "CTT", "AST") are also supported. However, their use is deprecated because the same abbreviation is often used for multiple time zones (for example, "CST" could be U.S. "Central Standard Time" and "China Standard Time"), and the Java platform can then only recognize one of them.

    There is a more complete list of these here, some of which are TZDB aliases, and some of which only exist in Java.

    If you wanted to add the Java legacy identifiers to moment-timezone, you could add them as links, and then your lists would look identical on both sides. For example:

    moment.tz.link('Asia/Shanghai|CTT');
    

    However, the better thing to do would probably be to not allow non-standard identifiers on the server-side in the first place.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • Yeah, I noticed the ones that are missing from moment are the 3 letter ones as you mentioned. By not allowing them, do you mean the only way is to remove them on server side Java to make both lists match? – lukforce Oct 13 '16 at 20:36
  • Sounds right. I would take the list returned from `getAvailableIDs`, and filter it. – Matt Johnson-Pint Oct 13 '16 at 21:37
  • That is what I did for now but I was still hoping for a solution where I can just have two matching lists without having to do this. Removing entries from this moderately long list multiple times does not look so good performance wise. – lukforce Oct 13 '16 at 21:58
0

The list size is different as you are effectively comparing two lists written two years apart.

If you want to source the very latest (2016g) timezone list for Java/ even to update what you get in Javascript, you can get them here https://www.iana.org/time-zones

You can modify the URLS listed on the site for older versions (2016f) if you need it.

Once you have the required data file, just load it into your application.

You can see here: http://www.oracle.com/technetwork/java/javase/tzdata-versions-138805.html With any of the JDKs 6/7/8. You would be limited to version 2016d.

UserF40
  • 3,533
  • 2
  • 23
  • 34
  • How do I load the newer version of tz data into my application? Do I use tzupdater on the Java that I am using to compile, or do I update the one for my application server (WebLogic in this case)? – lukforce Oct 11 '16 at 00:44
  • I used the tzupdater to update the Java list (on both the compiler and the application server), now the list is even bigger than before (was 620 now 629), while the moment list is 588. I checked the version and it's 2016f matching the version I found in my moment-timezone data file. Is there other way to synchronize the lists? – lukforce Oct 11 '16 at 01:20