Is there a way to get a timeZone with (only) a country code (valid ISO-3166 code)?
Asked Answered
S

8

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 ?

Stock answered 7/9, 2009 at 15:17 Comment(0)
S
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.

Saldana answered 7/9, 2009 at 16:0 Comment(1)
not only that, but the single arg Locale constructor specifies the language, not the country.Mozellemozes
T
5

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

Torrez answered 17/1, 2012 at 16:35 Comment(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.Strongarm
O
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?

Obscurantism answered 7/9, 2009 at 15:18 Comment(4)
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.Aymara
Yup - some countries have chosen to be sane when it comes to time zones :)Obscurantism
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.Aymara
@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 :)Obscurantism
C
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.

Coagulant answered 7/9, 2009 at 15:44 Comment(1)
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.Stock
H
0

There is no 1to1 mapping between ISO3 country code and a Timezone, since a country can have more than one.

As a workaround you can use a custom map like this one below for the countries you are interested in. Keep in mind that this map is totally questionable since there are no conventions on defining a timezone as "main" for a country as far as I know.

private static final Map<CountryEnum, String> mainTimeZones;
static {
    Map<CountryEnum, String> timezoneMap = new HashMap<>();
    timezoneMap.put(ARG, "America/Argentina/Buenos_Aires");
    timezoneMap.put(BGR, "Europe/Sofia");
    timezoneMap.put(BRA, "America/Sao_Paulo");
    timezoneMap.put(CHN, "Asia/Shanghai");
    timezoneMap.put(CZE, "Europe/Prague");
    timezoneMap.put(CHE, "Europe/Zurich");
    timezoneMap.put(DEU, "Europe/Berlin");
    timezoneMap.put(ESP, "Europe/Madrid");
    timezoneMap.put(FRA, "Europe/Paris");
    timezoneMap.put(GBR, "Europe/London");
    timezoneMap.put(HRV, "Europe/Zagreb");
    timezoneMap.put(IND, "Asia/Kolkata");
    timezoneMap.put(ITA, "Europe/Rome");
    timezoneMap.put(LTU, "Europe/Vilnius");
    timezoneMap.put(MNE, "Europe/Podgorica");
    timezoneMap.put(NLD, "Europe/Amsterdam");
    timezoneMap.put(POL, "Europe/Warsaw");
    timezoneMap.put(PRT, "Europe/Lisbon");
    timezoneMap.put(ROU, "Europe/Bucharest");
    timezoneMap.put(RUS, "Europe/Moscow");
    timezoneMap.put(SRB, "Europe/Belgrade");
    timezoneMap.put(SVK, "Europe/Bratislava");
    timezoneMap.put(TUR, "Europe/Istanbul");
    timezoneMap.put(USA, "America/New_York");
    timezoneMap.put(UKR, "Europe/Kiev");
    timezoneMap.put(HUN, "Europe/Budapest");
    timezoneMap.put(ALL, "Europe/Tirane");
    timezoneMap.put(CHL, "America/Santiago");
    timezoneMap.put(MEX, "America/Mexico_City");
    timezoneMap.put(EST, "Europe/Tallinn");
    timezoneMap.put(LVA, "Europe/Riga");
    timezoneMap.put(COL, "America/Bogota");
    ....
    mainTimeZones = Collections.unmodifiableMap(timezoneMap);
Histone answered 4/4, 2024 at 8:24 Comment(0)
E
-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

Emeraldemerge answered 14/12, 2014 at 11:59 Comment(1)
No, that will return TimeZone.getDefault()Navada
G
-2

The olson database contains all the mappings. Link

Search for the zone.tab file.

Gan answered 25/8, 2010 at 12:4 Comment(0)
A
-3

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"]
Accompanist answered 26/2, 2014 at 14:15 Comment(1)
this is a Java questionThereafter

© 2022 - 2025 — McMap. All rights reserved.