Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
417 views
in Technique[技术] by (71.8m points)

java - Get three-letter short timezone name (as opposed to four-letter)?

I've written an Android app that needs the short timezone name in which the handset is currently located.

I'm using the following code:

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

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

The timezone in this location is referred to by some as "Central European Time" (see Wikipedia's Time zones of Europe).

I'm passing timeZone off to a third-party system which very much insists on getting "CET" (not "HNEC"). Is there an API call I can rely on to return the three-letter (and, I think, "more modern") short timezone name?

As my code runs in more and more locations, I'm guessing this problem is going to occur elsewhere, too.

I'm really hoping to avoid maintaining some sort of map of three-letter to four-letter short timezone names.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I'm sorry, but you're probably stuck maintaining a mapping. The three-character are actually the older, not more modern version. From the classdocs:

    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.

If it were my problem, I'd get the list my third-party system supports and map it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...