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

查看:33
本文介绍了有没有办法使用(仅)国家代码(有效的 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 ...

有什么想法吗?

推荐答案

Locale 不是 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

获取使用默认时区和指定区域设置的日历.

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天全站免登陆