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