有没有办法用(仅)国家代码(有效的ISO-3166代码)获得时区? [英] Is there a way to get a timeZone with (only) a country code (valid ISO-3166 code)?

查看:360
本文介绍了有没有办法用(仅)国家代码(有效的ISO-3166代码)获得时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为用户获取TimeZone。

I'm trying to get a TimeZone for a user.

为此我有一个国家/地区代码是有效的ISO国家/地区代码。这些代码是ISO-3166定义的大写双字母代码。您可以在多个站点找到这些代码的完整列表,例如:
http://www.chemie.fu-berlin.de/diverse/doc/ISO_3166.html

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

我认为回应是不,因为这是一个多方面的关系...像美国这样的国家可能有很多时区......。这就是问题...

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

我试过类似的事情:

//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);
  }

但它总是返回服务器TimeZone ...

But it always return server TimeZone ...

任何想法?

推荐答案

A 区域设置不是 TimeZone ,反之亦然。查看您正在使用的方法的Javadoc - 第一行说

A Locale is not a TimeZone, and vice versa. Check the Javadoc for the method you're using - the very first line says


使用默认时区获取日历并指定locale。

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.

想想Jon所说的话 - 如果你知道在你设定用户的情况下你想要使用的时区来自美国,你可以打电话给 Calendar.getInstance 采用时区区域设置的方法。另一方面,如果你不能肯定地说你会在这里做什么,那么回到绘图板再考虑一下你的要求,而不是看你的实现。

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

如果您无法回答上一个问题,我认为大多数网站的标准资源是允许用户指定他们的首选时区(如果他们在服务器上有持久帐户)并将其默认为服务器的时区,如果他们没有说的话。如果他们没有持久帐户,并且他们有时会向您提供信息(例如XML上传),那么他们必须指定他们在请求中使用的时区,或者(可能更好)您要求始终使用UTC。

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.

这篇关于有没有办法用(仅)国家代码(有效的ISO-3166代码)获得时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆