Java 有没有办法使用(仅)国家代码(有效的 ISO-3166 代码)获取时区?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1389837/
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
Is there a way to get a timeZone with (only) a country code (valid ISO-3166 code)?
提问by ajeanson
I'm trying to get a TimeZone for a user.
我正在尝试为用户获取时区。
For this I have a country code which is a valid ISO Country Code. These codes are the upper-case, two-letter codes as defined by ISO-3166. You can find a full list of these codes at a number of sites, such as: http://www.chemie.fu-berlin.de/diverse/doc/ISO_3166.html
为此,我有一个国家代码,它是一个有效的 ISO 国家代码。这些代码是 ISO-3166 定义的大写两字母代码。您可以在许多站点上找到这些代码的完整列表,例如: http://www.chemie.fu-berlin.de/diverse/doc/ISO_3166.html
I think the response is "no because it's a manytomany relationship... there can be many timezone for a country like USA ...". That's the problem...
我认为答案是“不,因为这是一个多对多的关系……像美国这样的国家可能有很多时区……”。那就是问题所在...
I've tryied something like:
我尝试过类似的东西:
//CountryEnum contains ISO_3166 values (http://www.chemie.fu-berlin.de/diverse/doc/ISO_3166.html)
//List all country to test timezone:
for (int i = 0; i < CountryEnum.values().length; i++) {
String isoCountryCode = CountryEnum.values()[i].name();// Get the iso country code
Locale locale = new Locale(isoCountryCode);// Build a country specific locale
Calendar calendar = Calendar.getInstance(locale);// Build a calendar with the specific locale
String timeZone = calendar.getTimeZone().getDisplayName();// Build a timeZone with the calendar
System.out.println("LOCALE : "+locale+" / COUNTRY: "+isoCountryCode+" / TIMEZONE: "+timeZone);
}
But it always return server TimeZone ...
但它总是返回服务器 TimeZone ...
Any ideas ?
有任何想法吗 ?
回答by Jon Skeet
You're exactly right. There isno one time zone for the US - or Russia, or various other "big" countries (in terms of East/West).
你说得对。有是没有为美国一个时区-或俄罗斯或其他各种“大”国家(东/西方面)。
The US is certainly a simple example - what time zone would you want to use? America/Los_Angeles
? America/New_York
? Something else?
美国当然是一个简单的例子——您想使用哪个时区?America/Los_Angeles
? America/New_York
? 还有什么?
回答by Yishai
I'm not aware of anything that gives you this (as Jon pointed out the use case is rather limited) but you could build a Map using this data. The list would be a multi-map, so perhaps you would use that from Google Collections, or use your own ISO-code->List<String>
map.
我不知道有什么可以为您提供此信息(正如 Jon 指出的用例相当有限),但您可以使用此数据构建 Map 。该列表将是一个多地图,因此您可能会使用来自 Google Collections 的列表,或者使用您自己的 ISO-code->List<String>
地图。
Given the time zone string, you could create a TimeZone object and go from there. However, if the country has more than one timezone, you have to decide how to handle that.
给定时区字符串,您可以创建一个 TimeZone 对象并从那里开始。但是,如果该国家/地区有多个时区,则您必须决定如何处理。
回答by Andrzej Doyle
A Locale
is not a TimeZone
, and vice versa. Check the Javadocfor the method you're using - the very first line says
ALocale
不是 a TimeZone
,反之亦然。检查您正在使用的方法的Javadoc- 第一行说
Gets a calendar using the default time zone and specified locale.
使用默认时区和指定区域设置获取日历。
That's why you're getting the default timezone - since you didn't specify one when obtaining a Calendar.
这就是您获得默认时区的原因 - 因为您在获取日历时没有指定时区。
Think about what Jon said - if you know what timezone you would want to use in the situation where you've worked out a user is from the US, then you can call the Calendar.getInstance
method that takes a timezone anda locale. On the other hand, if you can't say definitely what you would do here, then go back to the drawing board and think about your requirements a little more, rather than looking at your implementation.
想想 Jon 所说的话 - 如果您知道在确定用户来自美国的情况下要使用哪个时区,那么您可以调用Calendar.getInstance
采用时区和区域设置的方法。另一方面,如果你不能明确地说出你会在这里做什么,那么回到绘图板并更多地考虑你的需求,而不是查看你的实现。
If you can't answer the previous question, I think the standard recourse of most websites is to allow users to specify their preferred timezone (if they have a persistent account on the server) and default them to the server's timezone if they haven't said otherwise. If they don't have persistent accounts, and they're supplying information to you with times in (e.g. an XML upload), then they will have to either specify what time zone they're using in the request, or (probably better) you mandate the use of UTC for all times.
如果你不能回答上一个问题,我认为大多数网站的标准资源是允许用户指定他们的首选时区(如果他们在服务器上有一个永久帐户),如果他们没有,则将它们默认为服务器的时区否则说。如果他们没有永久帐户,并且他们向您提供时间信息(例如 XML 上传),那么他们将必须指定他们在请求中使用的时区,或者(可能更好)您要求始终使用 UTC。
回答by Marcel Menz
回答by MB.
You can use ICU4J for this... See http://helpdesk.objects.com.au/java/can-i-find-all-available-timezones-for-a-country
您可以为此使用 ICU4J...请参阅http://helpdesk.objects.com.au/java/can-i-find-all-available-timezones-for-a-country
回答by Zack Xu
Have you tried the TZInfogem?
你试过TZInfogem 吗?
You can get the timezones of a country this way:
您可以通过以下方式获取国家/地区的时区:
>> TZInfo::Country.get("DE").zone_identifiers
=> ["Europe/Berlin", "Europe/Busingen"]
>> TZInfo::Country.get("CN").zone_identifiers
=> ["Asia/Shanghai", "Asia/Harbin", "Asia/Chongqing", "Asia/Urumqi", "Asia/Kashgar"]
>> TZInfo::Country.get("US").zone_identifiers
=> ["America/New_York", "America/Detroit", "America/Kentucky/Louisville", "America/Kentucky/Monticello", "America/Indiana/Indianapolis", "America/Indiana/Vincennes", "America/Indiana/Winamac", "America/Indiana/Marengo", "America/Indiana/Petersburg", "America/Indiana/Vevay", "America/Chicago", "America/Indiana/Tell_City", "America/Indiana/Knox", "America/Menominee", "America/North_Dakota/Center", "America/North_Dakota/New_Salem", "America/North_Dakota/Beulah", "America/Denver", "America/Boise", "America/Phoenix", "America/Los_Angeles", "America/Anchorage", "America/Juneau", "America/Sitka", "America/Yakutat", "America/Nome", "America/Adak", "America/Metlakatla", "Pacific/Honolulu"]
回答by Sergio Kosik
Here the solution which you are looking for:
这是您正在寻找的解决方案:
public String getTimeZoneByLocale(final String languageTag){
final Locale locale = Locale.forLanguageTag(languageTag);
final Calendar cal = Calendar.getInstance(locale);
final TimeZone timeZone = cal.getTimeZone();
return timeZone.getID();
}
The languageTag is code like en_US or ru_RU
languageTag 是类似 en_US 或 ru_RU 的代码