Is there any way to get TimeZone by only country name?

11,067

Solution 1

If I understand your question, you could try something like this -

String[] tzIds = TimeZone.getAvailableIDs();
List<String> al = new ArrayList<String>();
for (String timeZoneId : tzIds) {
  if (timeZoneId.startsWith("Canada")) {
    al.add(timeZoneId);
  }
}
System.out.println(al);

The output (slightly re-formatted) here is

[Canada/Pacific, Canada/Yukon, Canada/Mountain, Canada/Central, 
 Canada/East-Saskatchewan, Canada/Saskatchewan, Canada/Eastern, 
 Canada/Atlantic, Canada/Newfoundland]

Solution 2

You can do:

TimeZone tz = TimeZone.getTimeZone (id);

Where id has to be an Olson name of the form Area/Location, such as America/Los_Angeles.

You can get the available time zones using:

String[] ids = TimeZone.getAvailableIDs(0);

The official documentation: http://developer.android.com/reference/java/util/TimeZone.html

Share:
11,067
Muhidul Hassan
Author by

Muhidul Hassan

Updated on September 02, 2022

Comments

  • Muhidul Hassan
    Muhidul Hassan over 1 year

    Hi I know this question is asked many times but those question relate webservice. I am developing an app that shows timezone selecting country by user. For example I have list of all the countries and when a user select one country from them I want search TimeZOne by country name (May be by string "Canada"). If i search like this is it possible to get timezone from android device? Note: My application does not use internet connection. I want to these without internet connection.

    Apart from that is there any relevant way?

    • Matt Johnson-Pint
      Matt Johnson-Pint almost 10 years
      Make sure you realize that many countries have more than one time zone. You cannot just pick the time zone by the country name alone.