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




time zones list by country (9)

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 ?


java - Getting current date and time based on the locale (country)

    public static void main(String[] arg){
        String[] timeZoneIDList = TimeZone.getAvailableIDs();
        //List of Time Zones
        for(String timeZoneID:timeZoneIDList){
            System.out.println(timeZoneID);
        }
        //What time is it in Fiji
        Calendar fijiCalendar = Calendar.getInstance(TimeZone.getTimeZone("Pacific/Fiji"));
        System.out.println("Time in Fiji->"+fijiCalendar.get(Calendar.HOUR)+":"+fijiCalendar.get(Calendar.MINUTE));
    }

The link you gave to zone.tab is actually the source of the data you're looking for. Of course, you'd want to use one that is authoritative, rather than the copy on Wikipedia.

The IANA/Olson database is implemented in Java with the JodaTime library. But I don't believe they expose zone.tab via any classes. At least I couldn't find it. Please update me if I am incorrect in this.

You can always import the file yourself and use it as needed.

UPDATE

I know nothing about Android, but I found something that might make sense to you. There appears to be a function that exposes the zone.tab data in libcore.icu.TimeZoneNames.forLocale(locale)

I searched, but I couldn't find any reference documentation on this.


Get country for a given java TimeZone

In order to map between countries and timezones I used the ICU library. Using the com.ibm.icu.util.TimeZone.getAvailableIDs(countryCode) method I managed to map timezones to country codes.

public static Map<String, String> mapTimezonesToCountries() {
    Map<String, String> timezoneToCountry = new HashMap<>();

    String[] locales = Locale.getISOCountries();

    for (String countryCode : locales) {
        for (String id : com.ibm.icu.util.TimeZone.getAvailableIDs(countryCode))
        {
            // Add timezone to result map

            timezoneToCountry.put(id, countryCode);
        }

    }
    return timezoneToCountry;

}



We use .NET C# MVC and we needed a list of Windows Timezone for Each country.

So that for every registering user we could set the timezone automatically.

Most online databases provide IANA timezones and it's difficult to map to a specific country.

Using NodaTime we managed to create a Json which lists the following entries.

CountryName
2LetterIsoCountryCode
3LetterIsoCountryCode
IANATimezones
WindowsTimezones

You can download the generated JSON here.


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.


You cannot, there is no 1:1 relation from TimeZone to Country.
E.g there is also the timeZone "UTC"


I need a mapping list of cities to timezones- best way to get it?

Interesting question! These other three SO posts (41504, 55901 and 237023) mention a couple of leads. There's the Olson database (see the Wikipedia entry for description), but it looks more algorithmic/programmatic (e.g. if you know a county name, there's a rule for it for certain counties in Indiana).

I tried looking at EarthTools but it failed for St.Pierre/Miquelon, one of the good test cases (-3:00 rather the -3:30 for nearby Newfoundland which is what EarthTools says).

Some promising options: worldtimeengine.com has a free interactive version and an API which appears to be inexpensive (5000 queries per British pound), but the API only covers lat,long queries as inputs (not location as input, which is something the interactive version has).

Best lead: I'd look at geonames.org which looks like it has a nice rich API that you could use, and they have both a free (slow) version, and a commercial version for better performance.

p.s. here are some good test cases (from worldtimezone.com and Wikipedia's list of timezones, the map of time zones is also illustrative):

  • Caracas, Venezuela (-4:30)
  • Gander, Newfoundland (-3:30)
  • Tehran, Iran (+3:30)
  • Kabul, Afghanistan (+4:30)
  • Kathmandu, Nepal (+5:45)
  • Yangon, Myanmar (+6:30)
  • Chatham islands, New Zealand (+12:45)
  • Easter Island (-6)
  • St. Pierre and Miquelon (-3)
  • Stanley, Falkland Islands (-4)
  • Evansville, Indiana (-6=Central)
  • Ontario, Oregon (-7=Mountain)