java 获取三个字母的短时区名称(而不是四个字母)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2807197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get three-letter short timezone name (as opposed to four-letter)?
提问by lance
I've written an Android app that needs the short timezone name in which the handset is currently located.
我编写了一个 Android 应用程序,它需要手机当前所在的短时区名称。
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 Centralein French).
在芝加哥运行时,返回“CST”。在纽约,“EST”。在法国斯特拉斯堡,它正在返回“HNEC”(法语Heure Normale de l'Europe Centrale)。
The timezone in this location is referred to by some as "Central European Time" (see Wikipedia's Time zones of Europe).
这个位置的时区被一些人称为“中欧时间”(参见维基百科的欧洲时区)。
I'm passing timeZoneoff 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?
我正在使用timeZone第三方系统,该系统非常坚持要获得“CET”(而不是“HNEC”)。是否有我可以依赖的 API 调用来返回三个字母(我认为,“更现代”)的短时区名称?
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.
我真的希望避免维护某种三字母到四字母短时区名称的地图。
回答by CPerkins
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 deprecatedbecause 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.
- 为了与 JDK 1.1.x 兼容,还支持其他一些三字母时区 ID(例如“PST”、“CTT”、“AST”)。但是,不推荐使用它们,因为同一个缩写经常用于多个时区(例如,“CST”可以是美国“中部标准时间”和“ CN 标准时间”),并且 Java 平台只能识别其中之一他们。
If it were my problem, I'd get the list my third-party system supports and map it.
如果是我的问题,我会得到我的第三方系统支持的列表并映射它。
回答by Chris Stillwell
You could just use SimpleDateFormatto print the time zone in three letters or full name. For example:
您可以使用SimpleDateFormat以三个字母或全名打印时区。例如:
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());

