16

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

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

Any ideas ?

ajeanson
  • 2,580
  • 4
  • 19
  • 17

7 Answers7

6

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.

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.

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.

Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
  • not only that, but the single arg `Locale` constructor specifies the language, not the country. – akf Mar 08 '10 at 22:37
5

You can use ICU4J for this... See http://helpdesk.objects.com.au/java/can-i-find-all-available-timezones-for-a-country

MB.
  • 7,365
  • 6
  • 42
  • 42
  • 1
    That's handy, but the list returned seems to be in alphabetical order by name; it's a shame ICU4J doesn't provide a way to get the "least surprising timezone for the country code" for display purposes; most likely this would be that of the capital. – Richard Barnett Jul 18 '13 at 08:41
3

You're exactly right. There is no 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?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    Interestingly, there are other big countries on a single time zone; both India and China fall into this category. India would normally cover two time zones; China would cover 4 time zones. – Jonathan Leffler Sep 07 '09 at 15:25
  • 2
    Yup - some countries have chosen to be sane when it comes to time zones :) – Jon Skeet Sep 07 '09 at 15:38
  • 1
    Sanity - yes; of one sort. But in China, if the sun rises at 5 am on the east coast, it doesn't rise until about 8 am on the western edge of the Gobi desert. And if it sets at 7 pm on the east coast, it doesn't set until 10 pm in the west. – Jonathan Leffler Sep 07 '09 at 15:52
  • @Jonathan: That seems reasonable to me... and the benefits of not having to take a time zone into account when organizing meetings (etc) are *massive*. I have a grudge against time zones :) – Jon Skeet Sep 07 '09 at 16:11
0

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.

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.

Yishai
  • 90,445
  • 31
  • 189
  • 263
  • Exactly, it's the best solution for me. I think I will probably map timezones with a country, to have a unique timezone by country. – ajeanson Sep 08 '09 at 13:51
-1

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

Sergio Kosik
  • 192
  • 2
  • 6
-2

Have you tried the TZInfo gem?

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"]
Zack Xu
  • 11,505
  • 9
  • 70
  • 78
-2

The olson database contains all the mappings. Link

Search for the zone.tab file.

Marcel Menz
  • 1,075
  • 7
  • 17
  • 25