Friday, May 29, 2009

Getting Time Zone list in Java

In this post I wrote about how to get countries list in Java. I also wrote another post of how to get languages list in Java. Getting Time zones in Java is quite similar, but there are some differences.

Like Locale function: Locale.getISOCountries() and Locale.getISOLanguages(), TimeZone class also has a method for getting all time zone IDs: TimeZone.getAvailableIDs(). But, unlike countries and languages list, the time zone list contains many duplicate time zones, as well as undesired time zones like GMT+6 or country time zones like: Poland.

We would like to build neat time zones list, that contains only continent and city time zones. Something quite similar to Windows time zones. Achieving this goal is very simple. We only have to filter all time zones that does not start with continent. It can be done by matching the ID to the following regular expression pattern:

"^(Africa|America|Asia|Atlantic|Australia|Europe|Indian|Pacific)/.*"

After building a list of only desired time zones, we will sort it: first by time zone ID and then by time zone display name. We will implement a TimeZone comparator for the sort:

Collections.sort(timeZones, new Comparator<TimeZone>() {
 public int compare(final TimeZone t1, final TimeZone t2) {
   return t1.getID().compareTo(t2.getID());
 }
});

After building time zone list, we will add one more additional method that will allow us to get time zone name. The name will be constructed from 2 parts:

  • Time zone ID. This value appears with underscores (“_”), so we will replace them with spaces.
  • Time zone display name.

This is the method:

  public static String getName(TimeZone timeZone)
  {
    return timeZone.getID().replaceAll("_", " ") + " - " + timeZone.getDisplayName();
  }

And this is the entire class, also containing a small program to print all time zones:

package com.bashan.blog.timezone;
import java.util.*;
public class TimeZoneUtil {
  private static final String TIMEZONE_ID_PREFIXES =
      "^(Africa|America|Asia|Atlantic|Australia|Europe|Indian|Pacific)/.*";
  private static List<TimeZone> timeZones;
  public static List<TimeZone> getTimeZones() {
    if (timeZones == null) {
      timeZones = new ArrayList<TimeZone>();
      final String[] timeZoneIds = TimeZone.getAvailableIDs();
      for (final String id : timeZoneIds) {
        if (id.matches(TIMEZONE_ID_PREFIXES)) {
          timeZones.add(TimeZone.getTimeZone(id));
        }
      }
      Collections.sort(timeZones, new Comparator<TimeZone>() {
        public int compare(final TimeZone t1, final TimeZone t2) {
          return t1.getID().compareTo(t2.getID());
        }
      });
    }
    return timeZones;
  }
  public static String getName(TimeZone timeZone) {
    return timeZone.getID().replaceAll("_", " ") + " - " + timeZone.getDisplayName();
  }
  public static void main(String[] args) {
    timeZones = getTimeZones();
    for (TimeZone timeZone : timeZones) {
      System.out.println(getName(timeZone));
    }
  }
}

And the output of time zones list:

Africa/Abidjan - Greenwich Mean Time
Africa/Accra - Ghana Mean Time
Africa/Addis Ababa - Eastern African Time
Africa/Algiers - Central European Time
Africa/Asmara - Eastern African Time
Africa/Asmera - Eastern African Time
Africa/Bamako - Greenwich Mean Time
Africa/Bangui - Western African Time
Africa/Banjul - Greenwich Mean Time
Africa/Bissau - Greenwich Mean Time
Africa/Blantyre - Central African Time
Africa/Brazzaville - Western African Time
Africa/Bujumbura - Central African Time
Africa/Cairo - Eastern European Time
Africa/Casablanca - Western European Time
Africa/Ceuta - Central European Time
Africa/Conakry - Greenwich Mean Time
Africa/Dakar - Greenwich Mean Time
Africa/Dar es Salaam - Eastern African Time
Africa/Djibouti - Eastern African Time
Africa/Douala - Western African Time
Africa/El Aaiun - Western European Time
Africa/Freetown - Greenwich Mean Time
Africa/Gaborone - Central African Time
Africa/Harare - Central African Time
Africa/Johannesburg - South Africa Standard Time
Africa/Kampala - Eastern African Time
Africa/Khartoum - Eastern African Time
Africa/Kigali - Central African Time
Africa/Kinshasa - Western African Time
Africa/Lagos - Western African Time
Africa/Libreville - Western African Time
Africa/Lome - Greenwich Mean Time
Africa/Luanda - Western African Time
Africa/Lubumbashi - Central African Time
Africa/Lusaka - Central African Time
Africa/Malabo - Western African Time
Africa/Maputo - Central African Time
Africa/Maseru - South Africa Standard Time
Africa/Mbabane - South Africa Standard Time
Africa/Mogadishu - Eastern African Time
Africa/Monrovia - Greenwich Mean Time
Africa/Nairobi - Eastern African Time
Africa/Ndjamena - Western African Time
Africa/Niamey - Western African Time
Africa/Nouakchott - Greenwich Mean Time
Africa/Ouagadougou - Greenwich Mean Time
Africa/Porto-Novo - Western African Time
Africa/Sao Tome - Greenwich Mean Time
Africa/Timbuktu - Greenwich Mean Time
Africa/Tripoli - Eastern European Time
Africa/Tunis - Central European Time
Africa/Windhoek - Western African Time
America/Adak - Hawaii-Aleutian Standard Time
America/Anchorage - Alaska Standard Time
America/Anguilla - Atlantic Standard Time
America/Antigua - Atlantic Standard Time
America/Araguaina - Brasilia Time
America/Argentina/Buenos Aires - Argentine Time
America/Argentina/Catamarca - Argentine Time
America/Argentina/ComodRivadavia - Argentine Time
America/Argentina/Cordoba - Argentine Time
America/Argentina/Jujuy - Argentine Time
America/Argentina/La Rioja - Argentine Time
America/Argentina/Mendoza - Argentine Time
America/Argentina/Rio Gallegos - Argentine Time
America/Argentina/Salta - Argentine Time
America/Argentina/San Juan - Argentine Time
America/Argentina/San Luis - Argentine Time
America/Argentina/Tucuman - Argentine Time
America/Argentina/Ushuaia - Argentine Time
America/Aruba - Atlantic Standard Time
America/Asuncion - Paraguay Time
America/Atikokan - Eastern Standard Time
America/Atka - Hawaii-Aleutian Standard Time
America/Bahia - Brasilia Time
America/Barbados - Atlantic Standard Time
America/Belem - Brasilia Time
America/Belize - Central Standard Time
America/Blanc-Sablon - Atlantic Standard Time
America/Boa Vista - Amazon Time
America/Bogota - Colombia Time
America/Boise - Mountain Standard Time
America/Buenos Aires - Argentine Time
America/Cambridge Bay - Mountain Standard Time
America/Campo Grande - Amazon Time
America/Cancun - Central Standard Time
America/Caracas - Venezuela Time
America/Catamarca - Argentine Time
America/Cayenne - French Guiana Time
America/Cayman - Eastern Standard Time
America/Chicago - Central Standard Time
America/Chihuahua - Mountain Standard Time
America/Coral Harbour - Eastern Standard Time
America/Cordoba - Argentine Time
America/Costa Rica - Central Standard Time
America/Cuiaba - Amazon Time
America/Curacao - Atlantic Standard Time
America/Danmarkshavn - Greenwich Mean Time
America/Dawson - Pacific Standard Time
America/Dawson Creek - Mountain Standard Time
America/Denver - Mountain Standard Time
America/Detroit - Eastern Standard Time
America/Dominica - Atlantic Standard Time
America/Edmonton - Mountain Standard Time
America/Eirunepe - Amazon Time
America/El Salvador - Central Standard Time
America/Ensenada - Pacific Standard Time
America/Fort Wayne - Eastern Standard Time
America/Fortaleza - Brasilia Time
America/Glace Bay - Atlantic Standard Time
America/Godthab - Western Greenland Time
America/Goose Bay - Atlantic Standard Time
America/Grand Turk - Eastern Standard Time
America/Grenada - Atlantic Standard Time
America/Guadeloupe - Atlantic Standard Time
America/Guatemala - Central Standard Time
America/Guayaquil - Ecuador Time
America/Guyana - Guyana Time
America/Halifax - Atlantic Standard Time
America/Havana - Cuba Standard Time
America/Hermosillo - Mountain Standard Time
America/Indiana/Indianapolis - Eastern Standard Time
America/Indiana/Knox - Central Standard Time
America/Indiana/Marengo - Eastern Standard Time
America/Indiana/Petersburg - Eastern Standard Time
America/Indiana/Tell City - Central Standard Time
America/Indiana/Vevay - Eastern Standard Time
America/Indiana/Vincennes - Eastern Standard Time
America/Indiana/Winamac - Eastern Standard Time
America/Indianapolis - Eastern Standard Time
America/Inuvik - Mountain Standard Time
America/Iqaluit - Eastern Standard Time
America/Jamaica - Eastern Standard Time
America/Jujuy - Argentine Time
America/Juneau - Alaska Standard Time
America/Kentucky/Louisville - Eastern Standard Time
America/Kentucky/Monticello - Eastern Standard Time
America/Knox IN - Central Standard Time
America/La Paz - Bolivia Time
America/Lima - Peru Time
America/Los Angeles - Pacific Standard Time
America/Louisville - Eastern Standard Time
America/Maceio - Brasilia Time
America/Managua - Central Standard Time
America/Manaus - Amazon Time
America/Marigot - Atlantic Standard Time
America/Martinique - Atlantic Standard Time
America/Mazatlan - Mountain Standard Time
America/Mendoza - Argentine Time
America/Menominee - Central Standard Time
America/Merida - Central Standard Time
America/Mexico City - Central Standard Time
America/Miquelon - Pierre & Miquelon Standard Time
America/Moncton - Atlantic Standard Time
America/Monterrey - Central Standard Time
America/Montevideo - Uruguay Time
America/Montreal - Eastern Standard Time
America/Montserrat - Atlantic Standard Time
America/Nassau - Eastern Standard Time
America/New York - Eastern Standard Time
America/Nipigon - Eastern Standard Time
America/Nome - Alaska Standard Time
America/Noronha - Fernando de Noronha Time
America/North Dakota/Center - Central Standard Time
America/North Dakota/New Salem - Central Standard Time
America/Panama - Eastern Standard Time
America/Pangnirtung - Eastern Standard Time
America/Paramaribo - Suriname Time
America/Phoenix - Mountain Standard Time
America/Port-au-Prince - Eastern Standard Time
America/Port of Spain - Atlantic Standard Time
America/Porto Acre - Amazon Time
America/Porto Velho - Amazon Time
America/Puerto Rico - Atlantic Standard Time
America/Rainy River - Central Standard Time
America/Rankin Inlet - Central Standard Time
America/Recife - Brasilia Time
America/Regina - Central Standard Time
America/Resolute - Eastern Standard Time
America/Rio Branco - Amazon Time
America/Rosario - Argentine Time
America/Santarem - Brasilia Time
America/Santiago - Chile Time
America/Santo Domingo - Atlantic Standard Time
America/Sao Paulo - Brasilia Time
America/Scoresbysund - Eastern Greenland Time
America/Shiprock - Mountain Standard Time
America/St Barthelemy - Atlantic Standard Time
America/St Johns - Newfoundland Standard Time
America/St Kitts - Atlantic Standard Time
America/St Lucia - Atlantic Standard Time
America/St Thomas - Atlantic Standard Time
America/St Vincent - Atlantic Standard Time
America/Swift Current - Central Standard Time
America/Tegucigalpa - Central Standard Time
America/Thule - Atlantic Standard Time
America/Thunder Bay - Eastern Standard Time
America/Tijuana - Pacific Standard Time
America/Toronto - Eastern Standard Time
America/Tortola - Atlantic Standard Time
America/Vancouver - Pacific Standard Time
America/Virgin - Atlantic Standard Time
America/Whitehorse - Pacific Standard Time
America/Winnipeg - Central Standard Time
America/Yakutat - Alaska Standard Time
America/Yellowknife - Mountain Standard Time
Asia/Aden - Arabia Standard Time
Asia/Almaty - Alma-Ata Time
Asia/Amman - Eastern European Time
Asia/Anadyr - Anadyr Time
Asia/Aqtau - Aqtau Time
Asia/Aqtobe - Aqtobe Time
Asia/Ashgabat - Turkmenistan Time
Asia/Ashkhabad - Turkmenistan Time
Asia/Baghdad - Arabia Standard Time
Asia/Bahrain - Arabia Standard Time
Asia/Baku - Azerbaijan Time
Asia/Bangkok - Indochina Time
Asia/Beirut - Eastern European Time
Asia/Bishkek - Kirgizstan Time
Asia/Brunei - Brunei Time
Asia/Calcutta - India Standard Time
Asia/Choibalsan - Choibalsan Time
Asia/Chongqing - China Standard Time
Asia/Chungking - China Standard Time
Asia/Colombo - India Standard Time
Asia/Dacca - Bangladesh Time
Asia/Damascus - Eastern European Time
Asia/Dhaka - Bangladesh Time
Asia/Dili - Timor-Leste Time
Asia/Dubai - Gulf Standard Time
Asia/Dushanbe - Tajikistan Time
Asia/Gaza - Eastern European Time
Asia/Harbin - China Standard Time
Asia/Ho Chi Minh - Indochina Time
Asia/Hong Kong - Hong Kong Time
Asia/Hovd - Hovd Time
Asia/Irkutsk - Irkutsk Time
Asia/Istanbul - Eastern European Time
Asia/Jakarta - West Indonesia Time
Asia/Jayapura - East Indonesia Time
Asia/Jerusalem - Israel Standard Time
Asia/Kabul - Afghanistan Time
Asia/Kamchatka - Petropavlovsk-Kamchatski Time
Asia/Karachi - Pakistan Time
Asia/Kashgar - China Standard Time
Asia/Katmandu - Nepal Time
Asia/Kolkata - India Standard Time
Asia/Krasnoyarsk - Krasnoyarsk Time
Asia/Kuala Lumpur - Malaysia Time
Asia/Kuching - Malaysia Time
Asia/Kuwait - Arabia Standard Time
Asia/Macao - China Standard Time
Asia/Macau - China Standard Time
Asia/Magadan - Magadan Time
Asia/Makassar - Central Indonesia Time
Asia/Manila - Philippines Time
Asia/Muscat - Gulf Standard Time
Asia/Nicosia - Eastern European Time
Asia/Novosibirsk - Novosibirsk Time
Asia/Omsk - Omsk Time
Asia/Oral - Oral Time
Asia/Phnom Penh - Indochina Time
Asia/Pontianak - West Indonesia Time
Asia/Pyongyang - Korea Standard Time
Asia/Qatar - Arabia Standard Time
Asia/Qyzylorda - Qyzylorda Time
Asia/Rangoon - Myanmar Time
Asia/Riyadh - Arabia Standard Time
Asia/Riyadh87 - GMT+03:07
Asia/Riyadh88 - GMT+03:07
Asia/Riyadh89 - GMT+03:07
Asia/Saigon - Indochina Time
Asia/Sakhalin - Sakhalin Time
Asia/Samarkand - Uzbekistan Time
Asia/Seoul - Korea Standard Time
Asia/Shanghai - China Standard Time
Asia/Singapore - Singapore Time
Asia/Taipei - China Standard Time
Asia/Tashkent - Uzbekistan Time
Asia/Tbilisi - Georgia Time
Asia/Tehran - Iran Standard Time
Asia/Tel Aviv - Israel Standard Time
Asia/Thimbu - Bhutan Time
Asia/Thimphu - Bhutan Time
Asia/Tokyo - Japan Standard Time
Asia/Ujung Pandang - Central Indonesia Time
Asia/Ulaanbaatar - Ulaanbaatar Time
Asia/Ulan Bator - Ulaanbaatar Time
Asia/Urumqi - China Standard Time
Asia/Vientiane - Indochina Time
Asia/Vladivostok - Vladivostok Time
Asia/Yakutsk - Yakutsk Time
Asia/Yekaterinburg - Yekaterinburg Time
Asia/Yerevan - Armenia Time
Atlantic/Azores - Azores Time
Atlantic/Bermuda - Atlantic Standard Time
Atlantic/Canary - Western European Time
Atlantic/Cape Verde - Cape Verde Time
Atlantic/Faeroe - Western European Time
Atlantic/Faroe - Western European Time
Atlantic/Jan Mayen - Central European Time
Atlantic/Madeira - Western European Time
Atlantic/Reykjavik - Greenwich Mean Time
Atlantic/South Georgia - South Georgia Standard Time
Atlantic/St Helena - Greenwich Mean Time
Atlantic/Stanley - Falkland Is. Time
Australia/ACT - Eastern Standard Time (New South Wales)
Australia/Adelaide - Central Standard Time (South Australia)
Australia/Brisbane - Eastern Standard Time (Queensland)
Australia/Broken Hill - Central Standard Time (South Australia/New South Wales)
Australia/Canberra - Eastern Standard Time (New South Wales)
Australia/Currie - Eastern Standard Time (New South Wales)
Australia/Darwin - Central Standard Time (Northern Territory)
Australia/Eucla - Central Western Standard Time (Australia)
Australia/Hobart - Eastern Standard Time (Tasmania)
Australia/LHI - Lord Howe Standard Time
Australia/Lindeman - Eastern Standard Time (Queensland)
Australia/Lord Howe - Lord Howe Standard Time
Australia/Melbourne - Eastern Standard Time (Victoria)
Australia/NSW - Eastern Standard Time (New South Wales)
Australia/North - Central Standard Time (Northern Territory)
Australia/Perth - Western Standard Time (Australia)
Australia/Queensland - Eastern Standard Time (Queensland)
Australia/South - Central Standard Time (South Australia)
Australia/Sydney - Eastern Standard Time (New South Wales)
Australia/Tasmania - Eastern Standard Time (Tasmania)
Australia/Victoria - Eastern Standard Time (Victoria)
Australia/West - Western Standard Time (Australia)
Australia/Yancowinna - Central Standard Time (South Australia/New South Wales)
Europe/Amsterdam - Central European Time
Europe/Andorra - Central European Time
Europe/Athens - Eastern European Time
Europe/Belfast - Greenwich Mean Time
Europe/Belgrade - Central European Time
Europe/Berlin - Central European Time
Europe/Bratislava - Central European Time
Europe/Brussels - Central European Time
Europe/Bucharest - Eastern European Time
Europe/Budapest - Central European Time
Europe/Chisinau - Eastern European Time
Europe/Copenhagen - Central European Time
Europe/Dublin - Greenwich Mean Time
Europe/Gibraltar - Central European Time
Europe/Guernsey - Greenwich Mean Time
Europe/Helsinki - Eastern European Time
Europe/Isle of Man - Greenwich Mean Time
Europe/Istanbul - Eastern European Time
Europe/Jersey - Greenwich Mean Time
Europe/Kaliningrad - Eastern European Time
Europe/Kiev - Eastern European Time
Europe/Lisbon - Western European Time
Europe/Ljubljana - Central European Time
Europe/London - Greenwich Mean Time
Europe/Luxembourg - Central European Time
Europe/Madrid - Central European Time
Europe/Malta - Central European Time
Europe/Mariehamn - Eastern European Time
Europe/Minsk - Eastern European Time
Europe/Monaco - Central European Time
Europe/Moscow - Moscow Standard Time
Europe/Nicosia - Eastern European Time
Europe/Oslo - Central European Time
Europe/Paris - Central European Time
Europe/Podgorica - Central European Time
Europe/Prague - Central European Time
Europe/Riga - Eastern European Time
Europe/Rome - Central European Time
Europe/Samara - Samara Time
Europe/San Marino - Central European Time
Europe/Sarajevo - Central European Time
Europe/Simferopol - Eastern European Time
Europe/Skopje - Central European Time
Europe/Sofia - Eastern European Time
Europe/Stockholm - Central European Time
Europe/Tallinn - Eastern European Time
Europe/Tirane - Central European Time
Europe/Tiraspol - Eastern European Time
Europe/Uzhgorod - Eastern European Time
Europe/Vaduz - Central European Time
Europe/Vatican - Central European Time
Europe/Vienna - Central European Time
Europe/Vilnius - Eastern European Time
Europe/Volgograd - Volgograd Time
Europe/Warsaw - Central European Time
Europe/Zagreb - Central European Time
Europe/Zaporozhye - Eastern European Time
Europe/Zurich - Central European Time
Indian/Antananarivo - Eastern African Time
Indian/Chagos - Indian Ocean Territory Time
Indian/Christmas - Christmas Island Time
Indian/Cocos - Cocos Islands Time
Indian/Comoro - Eastern African Time
Indian/Kerguelen - French Southern & Antarctic Lands Time
Indian/Mahe - Seychelles Time
Indian/Maldives - Maldives Time
Indian/Mauritius - Mauritius Time
Indian/Mayotte - Eastern African Time
Indian/Reunion - Reunion Time
Pacific/Apia - West Samoa Time
Pacific/Auckland - New Zealand Standard Time
Pacific/Chatham - Chatham Standard Time
Pacific/Easter - Easter Is. Time
Pacific/Efate - Vanuatu Time
Pacific/Enderbury - Phoenix Is. Time
Pacific/Fakaofo - Tokelau Time
Pacific/Fiji - Fiji Time
Pacific/Funafuti - Tuvalu Time
Pacific/Galapagos - Galapagos Time
Pacific/Gambier - Gambier Time
Pacific/Guadalcanal - Solomon Is. Time
Pacific/Guam - Chamorro Standard Time
Pacific/Honolulu - Hawaii Standard Time
Pacific/Johnston - Hawaii Standard Time
Pacific/Kiritimati - Line Is. Time
Pacific/Kosrae - Kosrae Time
Pacific/Kwajalein - Marshall Islands Time
Pacific/Majuro - Marshall Islands Time
Pacific/Marquesas - Marquesas Time
Pacific/Midway - Samoa Standard Time
Pacific/Nauru - Nauru Time
Pacific/Niue - Niue Time
Pacific/Norfolk - Norfolk Time
Pacific/Noumea - New Caledonia Time
Pacific/Pago Pago - Samoa Standard Time
Pacific/Palau - Palau Time
Pacific/Pitcairn - Pitcairn Standard Time
Pacific/Ponape - Ponape Time
Pacific/Port Moresby - Papua New Guinea Time
Pacific/Rarotonga - Cook Is. Time
Pacific/Saipan - Chamorro Standard Time
Pacific/Samoa - Samoa Standard Time
Pacific/Tahiti - Tahiti Time
Pacific/Tarawa - Gilbert Is. Time
Pacific/Tongatapu - Tonga Time
Pacific/Truk - Truk Time
Pacific/Wake - Wake Time
Pacific/Wallis - Wallis & Futuna Time
Pacific/Yap - Truk Time
You can also press here to download the class.

2 comments:

  1. Hey, I'm not sure if you aware but using this code an inconsistency is generated. Take a look in the Argentine times (Corboda, Catamarca and Buenos Aires) they're duplicated in the result.

    ReplyDelete
  2. Hi Guilherme,

    I don't think there is an inconsistency.
    It only depends on what exactly do you want to achieve (Some may want cities to be combines, so may prefer it separate).
    Anyway, it is a good idea to make a code that knows to filter/combine duplicate hours on the same country. I will do it if I will find the time. Thanks for the note.

    ReplyDelete