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