Get the three-letter short name of the time zone (as opposed to four letters)?

I wrote an Android application that needs the short name of the time zone in which the phone is currently located.

I am using the following code:

String timeZone = Calendar.getInstance().getTimeZone().getDisplayName(false, TimeZone.SHORT); 

When working in Chicago, this returns "CST". In New York "EST". In Strasbourg, France , he returns "HNEC" ( Heure Normale de l'Europe Centrale in French ).

The time zone at this place is referred to by some as "Central European Time" (see Wikipedia Europe Time Zone ).

I pass timeZone to a third-party system that insists on getting a "CET" (not an "HNEC"). Is there an API call that I can rely on to return the three-letter (and, I think, “more modern”) short name of the time zone?

As my code works in an increasing number of locations, I assume this problem will occur elsewhere.

I really hope to avoid saving some kind of three-letter short name map with a four-letter time zone.

+4
source share
2 answers

Sorry, but you were probably stuck keeping your cartography. The three characters actually represent an older rather than a more modern version. From the docs class:

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

If this was my problem, I would get a list maintained by a third-party system and sketch it out.

+6
source

You can simply use SimpleDateFormat to print the time zone with three letters or a full name. For instance:

 DateFormat getTimeZoneShort = new SimpleDateFormat("z", Locale.US); String timeZoneShort = getTimeZoneShort.format(Calendar.getInstance().getTime()); DateFormat getTimeZoneLong = new SimpleDateFormat("zzzz", Locale.US); String timeZoneLong = getTimeZoneLong.format(Calendar.getInstance().getTime()); 
0
source

Source: https://habr.com/ru/post/1216511/


All Articles