Java Tutorial/Development/TimeZone — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 15:30, 31 мая 2010
Содержание
- 1 Converting Times Between Time Zones
- 2 Convert time between timezone
- 3 Create a Calendar object with the local time zone and set the UTC from japanCal
- 4 Create an instance using Japan"s time zone and set it with the local UTC
- 5 Get current TimeZone using Java Calendar
- 6 Get the foreign time
- 7 Get the time in the local time zone
- 8 Getting all the time zones IDs
- 9 Getting the Current Time in Another Time Zone
- 10 Given a time of 10am in Japan, get the local time
- 11 Timezone conversion routines
- 12 TimeZone.getTimeZone("America/New_York")
- 13 TimeZone.getTimeZone("Asia/Tokyo")
- 14 TimeZone.getTimeZone("Europe/Paris")
- 15 To obtain a reference to a TimeZone object corresponding to a given time zone ID
Converting Times Between Time Zones
import java.util.Calendar;
import java.util.GregorianCalendar;
public class Main {
public static void main(String[] argv) throws Exception {
// Create a Calendar object with the local time zone
Calendar local = new GregorianCalendar();
local.set(Calendar.HOUR_OF_DAY, 10); // 0..23
local.set(Calendar.MINUTE, 0);
local.set(Calendar.SECOND, 0);
}
}
Convert time between timezone
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;
public class Main {
public static void main(String[] args) {
Calendar localTime = Calendar.getInstance();
localTime.set(Calendar.HOUR, 17);
localTime.set(Calendar.MINUTE, 15);
localTime.set(Calendar.SECOND, 20);
int hour = localTime.get(Calendar.HOUR);
int minute = localTime.get(Calendar.MINUTE);
int second = localTime.get(Calendar.SECOND);
System.out.printf("Local time : %02d:%02d:%02d\n", hour, minute, second);
Calendar germanyTime = new GregorianCalendar(TimeZone.getTimeZone("Germany"));
germanyTime.setTimeInMillis(localTime.getTimeInMillis());
hour = germanyTime.get(Calendar.HOUR);
minute = germanyTime.get(Calendar.MINUTE);
second = germanyTime.get(Calendar.SECOND);
System.out.printf("Germany time: %02d:%02d:%02d\n", hour, minute, second);
}
}
Create a Calendar object with the local time zone and set the UTC from japanCal
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;
public class Main {
public static void main(String[] argv) throws Exception {
Calendar japanCal = new GregorianCalendar(TimeZone.getTimeZone("Japan"));
japanCal.set(Calendar.HOUR_OF_DAY, 10); // 0..23
japanCal.set(Calendar.MINUTE, 0);
japanCal.set(Calendar.SECOND, 0);
Calendar local = new GregorianCalendar();
local.setTimeInMillis(japanCal.getTimeInMillis());
}
}
Create an instance using Japan"s time zone and set it with the local UTC
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;
public class Main {
public static void main(String[] argv) throws Exception {
Calendar local = Calendar.getInstance();
Calendar japanCal = new GregorianCalendar(TimeZone.getTimeZone("Japan"));
japanCal.setTimeInMillis(local.getTimeInMillis());
}
}
Get current TimeZone using Java Calendar
import java.util.Calendar;
import java.util.TimeZone;
public class Main {
public static void main(String[] args) {
Calendar now = Calendar.getInstance();
TimeZone timeZone = now.getTimeZone();
System.out.println("Current TimeZone is : " + timeZone.getDisplayName());
}
}
Get the foreign time
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;
public class Main {
public static void main(String[] argv) throws Exception {
Calendar local = Calendar.getInstance();
Calendar japanCal = new GregorianCalendar(TimeZone.getTimeZone("Japan"));
japanCal.setTimeInMillis(local.getTimeInMillis());
int hour = japanCal.get(Calendar.HOUR); // 3
int minutes = japanCal.get(Calendar.MINUTE); // 0
int seconds = japanCal.get(Calendar.SECOND); // 0
boolean am = japanCal.get(Calendar.AM_PM) == Calendar.AM; // true
}
}
Get the time in the local time zone
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;
public class Main {
public static void main(String[] argv) throws Exception {
Calendar japanCal = new GregorianCalendar(TimeZone.getTimeZone("Japan"));
japanCal.set(Calendar.HOUR_OF_DAY, 10); // 0..23
japanCal.set(Calendar.MINUTE, 0);
japanCal.set(Calendar.SECOND, 0);
Calendar local = new GregorianCalendar();
local.setTimeInMillis(japanCal.getTimeInMillis());
int hour = local.get(Calendar.HOUR); // 5
int minutes = local.get(Calendar.MINUTE); // 0
int seconds = local.get(Calendar.SECOND); // 0
boolean am = local.get(Calendar.AM_PM) == Calendar.AM; // false
}
}
Getting all the time zones IDs
import java.util.TimeZone;
public class MainClass {
public static void main(String[] a) {
String[] ids = TimeZone.getAvailableIDs();
for (String id : ids) {
System.out.println(id);
}
}
}
Etc/GMT+12 Etc/GMT+11 MIT Pacific/Apia Pacific/Midway Pacific/Niue Pacific/Pago_Pago Pacific/Samoa US/Samoa America/Adak America/Atka Etc/GMT+10 HST Pacific/Fakaofo Pacific/Honolulu Pacific/Johnston Pacific/Rarotonga Pacific/Tahiti SystemV/HST10 US/Aleutian US/Hawaii Pacific/Marquesas AST America/Anchorage America/Juneau America/Nome America/Yakutat Etc/GMT+9 Pacific/Gambier SystemV/YST9 SystemV/YST9YDT US/Alaska America/Dawson America/Ensenada America/Los_Angeles America/Tijuana America/Vancouver America/Whitehorse Canada/Pacific Canada/Yukon Etc/GMT+8 Mexico/BajaNorte PST PST8PDT Pacific/Pitcairn SystemV/PST8 SystemV/PST8PDT US/Pacific US/Pacific-New America/Boise America/Cambridge_Bay America/Chihuahua America/Dawson_Creek America/Denver America/Edmonton America/Hermosillo America/Inuvik America/Mazatlan America/Phoenix America/Shiprock America/Yellowknife Canada/Mountain Etc/GMT+7 MST MST7MDT Mexico/BajaSur Navajo PNT SystemV/MST7 SystemV/MST7MDT US/Arizona US/Mountain America/Belize America/Cancun America/Chicago America/Costa_Rica America/El_Salvador America/Guatemala America/Managua America/Menominee America/Merida America/Mexico_City America/Monterrey America/North_Dakota/Center America/Rainy_River America/Rankin_Inlet America/Regina America/Swift_Current America/Tegucigalpa America/Winnipeg CST CST6CDT Canada/Central Canada/East-Saskatchewan Canada/Saskatchewan Chile/EasterIsland Etc/GMT+6 Mexico/General Pacific/Easter Pacific/Galapagos SystemV/CST6 SystemV/CST6CDT US/Central America/Bogota America/Cayman America/Detroit America/Eirunepe America/Fort_Wayne America/Grand_Turk America/Guayaquil America/Havana America/Indiana/Indianapolis America/Indiana/Knox America/Indiana/Marengo America/Indiana/Vevay America/Indianapolis America/Iqaluit America/Jamaica America/Kentucky/Louisville America/Kentucky/Monticello America/Knox_IN America/Lima America/Louisville America/Montreal America/Nassau America/New_York America/Nipigon America/Panama America/Pangnirtung America/Port-au-Prince America/Porto_Acre America/Rio_Branco America/Thunder_Bay America/Toronto Brazil/Acre Canada/Eastern Cuba EST EST5EDT Etc/GMT+5 IET Jamaica SystemV/EST5 SystemV/EST5EDT US/East-Indiana US/Eastern US/Indiana-Starke US/Michigan America/Anguilla America/Antigua America/Aruba America/Asuncion America/Barbados America/Boa_Vista America/Campo_Grande America/Caracas America/Cuiaba America/Curacao America/Dominica America/Glace_Bay America/Goose_Bay America/Grenada America/Guadeloupe America/Guyana America/Halifax America/La_Paz America/Manaus America/Martinique America/Montserrat America/Port_of_Spain America/Porto_Velho America/Puerto_Rico America/Santiago America/Santo_Domingo America/St_Kitts America/St_Lucia America/St_Thomas America/St_Vincent America/Thule America/Tortola America/Virgin Antarctica/Palmer Atlantic/Bermuda Atlantic/Stanley Brazil/West Canada/Atlantic Chile/Continental Etc/GMT+4 PRT SystemV/AST4 SystemV/AST4ADT America/St_Johns CNT Canada/Newfoundland AGT America/Araguaina America/Bahia America/Belem America/Buenos_Aires America/Catamarca America/Cayenne America/Cordoba America/Fortaleza America/Godthab America/Jujuy America/Maceio America/Mendoza America/Miquelon America/Montevideo America/Paramaribo America/Recife America/Rosario America/Sao_Paulo Antarctica/Rothera BET Brazil/East Etc/GMT+3 America/Noronha Atlantic/South_Georgia Brazil/DeNoronha Etc/GMT+2 America/Scoresbysund Atlantic/Azores Atlantic/Cape_Verde Etc/GMT+1 Africa/Abidjan Africa/Accra Africa/Bamako Africa/Banjul Africa/Bissau Africa/Casablanca Africa/Conakry Africa/Dakar Africa/El_Aaiun Africa/Freetown Africa/Lome Africa/Monrovia Africa/Nouakchott Africa/Ouagadougou Africa/Sao_Tome Africa/Timbuktu America/Danmarkshavn Atlantic/Canary Atlantic/Faeroe Atlantic/Madeira Atlantic/Reykjavik Atlantic/St_Helena Eire Etc/GMT Etc/GMT+0 Etc/GMT-0 Etc/GMT0 Etc/Greenwich Etc/UCT Etc/UTC Etc/Universal Etc/Zulu Europe/Belfast Europe/Dublin Europe/Lisbon Europe/London GB GB-Eire GMT GMT0 Greenwich Iceland Portugal UCT UTC Universal WET Zulu Africa/Algiers Africa/Bangui Africa/Brazzaville Africa/Ceuta Africa/Douala Africa/Kinshasa Africa/Lagos Africa/Libreville Africa/Luanda Africa/Malabo Africa/Ndjamena Africa/Niamey Africa/Porto-Novo Africa/Tunis Africa/Windhoek Arctic/Longyearbyen Atlantic/Jan_Mayen CET ECT Etc/GMT-1 Europe/Amsterdam Europe/Andorra Europe/Belgrade Europe/Berlin Europe/Bratislava Europe/Brussels Europe/Budapest Europe/Copenhagen Europe/Gibraltar Europe/Ljubljana Europe/Luxembourg Europe/Madrid Europe/Malta Europe/Monaco Europe/Oslo Europe/Paris Europe/Prague Europe/Rome Europe/San_Marino Europe/Sarajevo Europe/Skopje Europe/Stockholm Europe/Tirane Europe/Vaduz Europe/Vatican Europe/Vienna Europe/Warsaw Europe/Zagreb Europe/Zurich MET Poland ART Africa/Blantyre Africa/Bujumbura Africa/Cairo Africa/Gaborone Africa/Harare Africa/Johannesburg Africa/Kigali Africa/Lubumbashi Africa/Lusaka Africa/Maputo Africa/Maseru Africa/Mbabane Africa/Tripoli Asia/Amman Asia/Beirut Asia/Damascus Asia/Gaza Asia/Istanbul Asia/Jerusalem Asia/Nicosia Asia/Tel_Aviv CAT EET Egypt Etc/GMT-2 Europe/Athens Europe/Bucharest Europe/Chisinau Europe/Helsinki Europe/Istanbul Europe/Kaliningrad Europe/Kiev Europe/Minsk Europe/Nicosia Europe/Riga Europe/Simferopol Europe/Sofia Europe/Tallinn Europe/Tiraspol Europe/Uzhgorod Europe/Vilnius Europe/Zaporozhye Israel Libya Turkey Africa/Addis_Ababa Africa/Asmera Africa/Dar_es_Salaam Africa/Djibouti Africa/Kampala Africa/Khartoum Africa/Mogadishu Africa/Nairobi Antarctica/Syowa Asia/Aden Asia/Baghdad Asia/Bahrain Asia/Kuwait Asia/Qatar Asia/Riyadh EAT Etc/GMT-3 Europe/Moscow Indian/Antananarivo Indian/Comoro Indian/Mayotte W-SU Asia/Riyadh87 Asia/Riyadh88 Asia/Riyadh89 Mideast/Riyadh87 Mideast/Riyadh88 Mideast/Riyadh89 Asia/Tehran Iran Asia/Aqtau Asia/Baku Asia/Dubai Asia/Muscat Asia/Oral Asia/Tbilisi Asia/Yerevan Etc/GMT-4 Europe/Samara Indian/Mahe Indian/Mauritius Indian/Reunion NET Asia/Kabul Asia/Aqtobe Asia/Ashgabat Asia/Ashkhabad Asia/Bishkek Asia/Dushanbe Asia/Karachi Asia/Samarkand Asia/Tashkent Asia/Yekaterinburg Etc/GMT-5 Indian/Kerguelen Indian/Maldives PLT Asia/Calcutta IST Asia/Katmandu Antarctica/Mawson Antarctica/Vostok Asia/Almaty Asia/Colombo Asia/Dacca Asia/Dhaka Asia/Novosibirsk Asia/Omsk Asia/Qyzylorda Asia/Thimbu Asia/Thimphu BST Etc/GMT-6 Indian/Chagos Asia/Rangoon Indian/Cocos Antarctica/Davis Asia/Bangkok Asia/Hovd Asia/Jakarta Asia/Krasnoyarsk Asia/Phnom_Penh Asia/Pontianak Asia/Saigon Asia/Vientiane Etc/GMT-7 Indian/Christmas VST Antarctica/Casey Asia/Brunei Asia/Chongqing Asia/Chungking Asia/Harbin Asia/Hong_Kong Asia/Irkutsk Asia/Kashgar Asia/Kuala_Lumpur Asia/Kuching Asia/Macao Asia/Macau Asia/Makassar Asia/Manila Asia/Shanghai Asia/Singapore Asia/Taipei Asia/Ujung_Pandang Asia/Ulaanbaatar Asia/Ulan_Bator Asia/Urumqi Australia/Perth Australia/West CTT Etc/GMT-8 Hongkong PRC Singapore Asia/Choibalsan Asia/Dili Asia/Jayapura Asia/Pyongyang Asia/Seoul Asia/Tokyo Asia/Yakutsk Etc/GMT-9 JST Japan Pacific/Palau ROK ACT Australia/Adelaide Australia/Broken_Hill Australia/Darwin Australia/North Australia/South Australia/Yancowinna AET Antarctica/DumontDUrville Asia/Sakhalin Asia/Vladivostok Australia/ACT Australia/Brisbane Australia/Canberra Australia/Hobart Australia/Lindeman Australia/Melbourne Australia/NSW Australia/Queensland Australia/Sydney Australia/Tasmania Australia/Victoria Etc/GMT-10 Pacific/Guam Pacific/Port_Moresby Pacific/Saipan Pacific/Truk Pacific/Yap Australia/LHI Australia/Lord_Howe Asia/Magadan Etc/GMT-11 Pacific/Efate Pacific/Guadalcanal Pacific/Kosrae Pacific/Noumea Pacific/Ponape SST Pacific/Norfolk Antarctica/McMurdo Antarctica/South_Pole Asia/Anadyr Asia/Kamchatka Etc/GMT-12 Kwajalein NST NZ Pacific/Auckland Pacific/Fiji Pacific/Funafuti Pacific/Kwajalein Pacific/Majuro Pacific/Nauru Pacific/Tarawa Pacific/Wake Pacific/Wallis NZ-CHAT Pacific/Chatham Etc/GMT-13 Pacific/Enderbury Pacific/Tongatapu Etc/GMT-14 Pacific/Kiritimati
Getting the Current Time in Another Time Zone
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;
public class Main {
public static void main(String[] argv) throws Exception {
// Get the current time in Hong Kong
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("Hongkong"));
int hour12 = cal.get(Calendar.HOUR); // 0..11
int minutes = cal.get(Calendar.MINUTE); // 0..59
int seconds = cal.get(Calendar.SECOND); // 0..59
boolean am = cal.get(Calendar.AM_PM) == Calendar.AM;
// Get the current hour-of-day at GMT
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
int hour24 = cal.get(Calendar.HOUR_OF_DAY); // 0..23
// Get the current local hour-of-day
cal.setTimeZone(TimeZone.getDefault());
hour24 = cal.get(Calendar.HOUR_OF_DAY); // 0..23
}
}
Given a time of 10am in Japan, get the local time
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;
public class Main {
public static void main(String[] argv) throws Exception {
Calendar japanCal = new GregorianCalendar(TimeZone.getTimeZone("Japan"));
japanCal.set(Calendar.HOUR_OF_DAY, 10); // 0..23
japanCal.set(Calendar.MINUTE, 0);
japanCal.set(Calendar.SECOND, 0);
}
}
Timezone conversion routines
/*
Copyright (C) 2002 MySQL AB
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.TimeZone;
/**
* Timezone conversion routines
*
* @author Mark Matthews
*/
public class TimeUtil
{
//~ Instance/static variables .............................................
static TimeZone GMT_TIMEZONE = TimeZone.getTimeZone("GMT");
static final HashMap TIMEZONE_MAPPINGS;
//~ Initializers ..........................................................
static {
TIMEZONE_MAPPINGS = new HashMap(175);
//
// Windows Mappings
//
TIMEZONE_MAPPINGS.put("Romance", "Europe/Paris");
TIMEZONE_MAPPINGS.put("Romance Standard Time", "Europe/Paris");
TIMEZONE_MAPPINGS.put("Warsaw", "Europe/Warsaw");
TIMEZONE_MAPPINGS.put("Central Europe", "Europe/Prague");
TIMEZONE_MAPPINGS.put("Central Europe Standard Time", "Europe/Prague");
TIMEZONE_MAPPINGS.put("Prague Bratislava", "Europe/Prague");
TIMEZONE_MAPPINGS.put("W. Central Africa Standard Time",
"Africa/Luanda");
TIMEZONE_MAPPINGS.put("FLE", "Europe/Helsinki");
TIMEZONE_MAPPINGS.put("FLE Standard Time", "Europe/Helsinki");
TIMEZONE_MAPPINGS.put("GFT", "Europe/Athens");
TIMEZONE_MAPPINGS.put("GFT Standard Time", "Europe/Athens");
TIMEZONE_MAPPINGS.put("GTB", "Europe/Athens");
TIMEZONE_MAPPINGS.put("GTB Standard Time", "Europe/Athens");
TIMEZONE_MAPPINGS.put("Israel", "Asia/Jerusalem");
TIMEZONE_MAPPINGS.put("Israel Standard Time", "Asia/Jerusalem");
TIMEZONE_MAPPINGS.put("Arab", "Asia/Riyadh");
TIMEZONE_MAPPINGS.put("Arab Standard Time", "Asia/Riyadh");
TIMEZONE_MAPPINGS.put("Arabic Standard Time", "Asia/Baghdad");
TIMEZONE_MAPPINGS.put("E. Africa", "Africa/Nairobi");
TIMEZONE_MAPPINGS.put("E. Africa Standard Time", "Africa/Nairobi");
TIMEZONE_MAPPINGS.put("Saudi Arabia", "Asia/Riyadh");
TIMEZONE_MAPPINGS.put("Saudi Arabia Standard Time", "Asia/Riyadh");
TIMEZONE_MAPPINGS.put("Iran", "Asia/Tehran");
TIMEZONE_MAPPINGS.put("Iran Standard Time", "Asia/Tehran");
TIMEZONE_MAPPINGS.put("Afghanistan", "Asia/Kabul");
TIMEZONE_MAPPINGS.put("Afghanistan Standard Time", "Asia/Kabul");
TIMEZONE_MAPPINGS.put("India", "Asia/Calcutta");
TIMEZONE_MAPPINGS.put("India Standard Time", "Asia/Calcutta");
TIMEZONE_MAPPINGS.put("Myanmar Standard Time", "Asia/Rangoon");
TIMEZONE_MAPPINGS.put("Nepal Standard Time", "Asia/Katmandu");
TIMEZONE_MAPPINGS.put("Sri Lanka", "Asia/Colombo");
TIMEZONE_MAPPINGS.put("Sri Lanka Standard Time", "Asia/Colombo");
TIMEZONE_MAPPINGS.put("Beijing", "Asia/Shanghai");
TIMEZONE_MAPPINGS.put("China", "Asia/Shanghai");
TIMEZONE_MAPPINGS.put("China Standard Time", "Asia/Shanghai");
TIMEZONE_MAPPINGS.put("AUS Central", "Australia/Darwin");
TIMEZONE_MAPPINGS.put("AUS Central Standard Time", "Australia/Darwin");
TIMEZONE_MAPPINGS.put("Cen. Australia", "Australia/Adelaide");
TIMEZONE_MAPPINGS.put("Cen. Australia Standard Time",
"Australia/Adelaide");
TIMEZONE_MAPPINGS.put("Vladivostok", "Asia/Vladivostok");
TIMEZONE_MAPPINGS.put("Vladivostok Standard Time", "Asia/Vladivostok");
TIMEZONE_MAPPINGS.put("West Pacific", "Pacific/Guam");
TIMEZONE_MAPPINGS.put("West Pacific Standard Time", "Pacific/Guam");
TIMEZONE_MAPPINGS.put("E. South America", "America/Sao_Paulo");
TIMEZONE_MAPPINGS.put("E. South America Standard Time",
"America/Sao_Paulo");
TIMEZONE_MAPPINGS.put("Greenland Standard Time", "America/Godthab");
TIMEZONE_MAPPINGS.put("Newfoundland", "America/St_Johns");
TIMEZONE_MAPPINGS.put("Newfoundland Standard Time", "America/St_Johns");
TIMEZONE_MAPPINGS.put("Pacific SA", "America/Caracas");
TIMEZONE_MAPPINGS.put("Pacific SA Standard Time", "America/Caracas");
TIMEZONE_MAPPINGS.put("SA Western", "America/Caracas");
TIMEZONE_MAPPINGS.put("SA Western Standard Time", "America/Caracas");
TIMEZONE_MAPPINGS.put("SA Pacific", "America/Bogota");
TIMEZONE_MAPPINGS.put("SA Pacific Standard Time", "America/Bogota");
TIMEZONE_MAPPINGS.put("US Eastern", "America/Indianapolis");
TIMEZONE_MAPPINGS.put("US Eastern Standard Time",
"America/Indianapolis");
TIMEZONE_MAPPINGS.put("Central America Standard Time",
"America/Regina");
TIMEZONE_MAPPINGS.put("Mexico", "America/Mexico_City");
TIMEZONE_MAPPINGS.put("Mexico Standard Time", "America/Mexico_City");
TIMEZONE_MAPPINGS.put("Canada Central", "America/Regina");
TIMEZONE_MAPPINGS.put("Canada Central Standard Time", "America/Regina");
TIMEZONE_MAPPINGS.put("US Mountain", "America/Phoenix");
TIMEZONE_MAPPINGS.put("US Mountain Standard Time", "America/Phoenix");
TIMEZONE_MAPPINGS.put("GMT", "Europe/London");
TIMEZONE_MAPPINGS.put("GMT Standard Time", "Europe/London");
TIMEZONE_MAPPINGS.put("Ekaterinburg", "Asia/Yekaterinburg");
TIMEZONE_MAPPINGS.put("Ekaterinburg Standard Time",
"Asia/Yekaterinburg");
TIMEZONE_MAPPINGS.put("West Asia", "Asia/Karachi");
TIMEZONE_MAPPINGS.put("West Asia Standard Time", "Asia/Karachi");
TIMEZONE_MAPPINGS.put("Central Asia", "Asia/Dhaka");
TIMEZONE_MAPPINGS.put("Central Asia Standard Time", "Asia/Dhaka");
TIMEZONE_MAPPINGS.put("N. Central Asia Standard Time",
"Asia/Novosibirsk");
TIMEZONE_MAPPINGS.put("Bangkok", "Asia/Bangkok");
TIMEZONE_MAPPINGS.put("Bangkok Standard Time", "Asia/Bangkok");
TIMEZONE_MAPPINGS.put("North Asia Standard Time", "Asia/Krasnoyarsk");
TIMEZONE_MAPPINGS.put("SE Asia", "Asia/Bangkok");
TIMEZONE_MAPPINGS.put("SE Asia Standard Time", "Asia/Bangkok");
TIMEZONE_MAPPINGS.put("North Asia East Standard Time",
"Asia/Ulaanbaatar");
TIMEZONE_MAPPINGS.put("Singapore", "Asia/Singapore");
TIMEZONE_MAPPINGS.put("Singapore Standard Time", "Asia/Singapore");
TIMEZONE_MAPPINGS.put("Taipei", "Asia/Taipei");
TIMEZONE_MAPPINGS.put("Taipei Standard Time", "Asia/Taipei");
TIMEZONE_MAPPINGS.put("W. Australia", "Australia/Perth");
TIMEZONE_MAPPINGS.put("W. Australia Standard Time", "Australia/Perth");
TIMEZONE_MAPPINGS.put("Korea", "Asia/Seoul");
TIMEZONE_MAPPINGS.put("Korea Standard Time", "Asia/Seoul");
TIMEZONE_MAPPINGS.put("Tokyo", "Asia/Tokyo");
TIMEZONE_MAPPINGS.put("Tokyo Standard Time", "Asia/Tokyo");
TIMEZONE_MAPPINGS.put("Yakutsk", "Asia/Yakutsk");
TIMEZONE_MAPPINGS.put("Yakutsk Standard Time", "Asia/Yakutsk");
TIMEZONE_MAPPINGS.put("Central European", "Europe/Belgrade");
TIMEZONE_MAPPINGS.put("Central European Standard Time",
"Europe/Belgrade");
TIMEZONE_MAPPINGS.put("W. Europe", "Europe/Berlin");
TIMEZONE_MAPPINGS.put("W. Europe Standard Time", "Europe/Berlin");
TIMEZONE_MAPPINGS.put("Tasmania", "Australia/Hobart");
TIMEZONE_MAPPINGS.put("Tasmania Standard Time", "Australia/Hobart");
TIMEZONE_MAPPINGS.put("AUS Eastern", "Australia/Sydney");
TIMEZONE_MAPPINGS.put("AUS Eastern Standard Time", "Australia/Sydney");
TIMEZONE_MAPPINGS.put("E. Australia", "Australia/Brisbane");
TIMEZONE_MAPPINGS.put("E. Australia Standard Time",
"Australia/Brisbane");
TIMEZONE_MAPPINGS.put("Sydney Standard Time", "Australia/Sydney");
TIMEZONE_MAPPINGS.put("Central Pacific", "Pacific/Guadalcanal");
TIMEZONE_MAPPINGS.put("Central Pacific Standard Time",
"Pacific/Guadalcanal");
TIMEZONE_MAPPINGS.put("Dateline", "Pacific/Majuro");
TIMEZONE_MAPPINGS.put("Dateline Standard Time", "Pacific/Majuro");
TIMEZONE_MAPPINGS.put("Fiji", "Pacific/Fiji");
TIMEZONE_MAPPINGS.put("Fiji Standard Time", "Pacific/Fiji");
TIMEZONE_MAPPINGS.put("Samoa", "Pacific/Apia");
TIMEZONE_MAPPINGS.put("Samoa Standard Time", "Pacific/Apia");
TIMEZONE_MAPPINGS.put("Hawaiian", "Pacific/Honolulu");
TIMEZONE_MAPPINGS.put("Hawaiian Standard Time", "Pacific/Honolulu");
TIMEZONE_MAPPINGS.put("Alaskan", "America/Anchorage");
TIMEZONE_MAPPINGS.put("Alaskan Standard Time", "America/Anchorage");
TIMEZONE_MAPPINGS.put("Pacific", "America/Los_Angeles");
TIMEZONE_MAPPINGS.put("Pacific Standard Time", "America/Los_Angeles");
TIMEZONE_MAPPINGS.put("Mexico Standard Time 2", "America/Chihuahua");
TIMEZONE_MAPPINGS.put("Mountain", "America/Denver");
TIMEZONE_MAPPINGS.put("Mountain Standard Time", "America/Denver");
TIMEZONE_MAPPINGS.put("Central", "America/Chicago");
TIMEZONE_MAPPINGS.put("Central Standard Time", "America/Chicago");
TIMEZONE_MAPPINGS.put("Eastern", "America/New_York");
TIMEZONE_MAPPINGS.put("Eastern Standard Time", "America/New_York");
TIMEZONE_MAPPINGS.put("E. Europe", "Europe/Bucharest");
TIMEZONE_MAPPINGS.put("E. Europe Standard Time", "Europe/Bucharest");
TIMEZONE_MAPPINGS.put("Egypt", "Africa/Cairo");
TIMEZONE_MAPPINGS.put("Egypt Standard Time", "Africa/Cairo");
TIMEZONE_MAPPINGS.put("South Africa", "Africa/Harare");
TIMEZONE_MAPPINGS.put("South Africa Standard Time", "Africa/Harare");
TIMEZONE_MAPPINGS.put("Atlantic", "America/Halifax");
TIMEZONE_MAPPINGS.put("Atlantic Standard Time", "America/Halifax");
TIMEZONE_MAPPINGS.put("SA Eastern", "America/Buenos_Aires");
TIMEZONE_MAPPINGS.put("SA Eastern Standard Time",
"America/Buenos_Aires");
TIMEZONE_MAPPINGS.put("Mid-Atlantic", "Atlantic/South_Georgia");
TIMEZONE_MAPPINGS.put("Mid-Atlantic Standard Time",
"Atlantic/South_Georgia");
TIMEZONE_MAPPINGS.put("Azores", "Atlantic/Azores");
TIMEZONE_MAPPINGS.put("Azores Standard Time", "Atlantic/Azores");
TIMEZONE_MAPPINGS.put("Cape Verde Standard Time",
"Atlantic/Cape_Verde");
TIMEZONE_MAPPINGS.put("Russian", "Europe/Moscow");
TIMEZONE_MAPPINGS.put("Russian Standard Time", "Europe/Moscow");
TIMEZONE_MAPPINGS.put("New Zealand", "Pacific/Auckland");
TIMEZONE_MAPPINGS.put("New Zealand Standard Time", "Pacific/Auckland");
TIMEZONE_MAPPINGS.put("Tonga Standard Time", "Pacific/Tongatapu");
TIMEZONE_MAPPINGS.put("Arabian", "Asia/Muscat");
TIMEZONE_MAPPINGS.put("Arabian Standard Time", "Asia/Muscat");
TIMEZONE_MAPPINGS.put("Caucasus", "Asia/Tbilisi");
TIMEZONE_MAPPINGS.put("Caucasus Standard Time", "Asia/Tbilisi");
TIMEZONE_MAPPINGS.put("GMT Standard Time", "GMT");
TIMEZONE_MAPPINGS.put("Greenwich", "GMT");
TIMEZONE_MAPPINGS.put("Greenwich Standard Time", "GMT");
}
//~ Methods ...............................................................
public static String getCanoncialTimezone(String timezoneStr)
{
if (timezoneStr == null) {
return null;
}
timezoneStr = timezoneStr.trim();
// Fix windows Daylight/Standard shift JDK doesn"t map these (doh)
String timezoneStrUC = timezoneStr.toUpperCase();
int daylightIndex = timezoneStrUC.indexOf("DAYLIGHT");
if (daylightIndex != -1) {
StringBuffer timezoneBuf = new StringBuffer();
timezoneBuf.append(timezoneStr.substring(0, daylightIndex));
timezoneBuf.append("Standard");
timezoneBuf.append(timezoneStr.substring(
daylightIndex + "DAYLIGHT".length(),
timezoneStr.length()));
timezoneStr = timezoneBuf.toString();
}
return (String)TIMEZONE_MAPPINGS.get(timezoneStr);
}
public static Timestamp changeTimezone(Timestamp tstamp, TimeZone fromTz,
TimeZone toTz)
{
/*
// Convert the timestamp to the GMT timezone
Calendar fromCal = Calendar.getInstance(fromTz);
fromCal.setTime(tstamp);
int fromOffset =
fromCal.get(Calendar.ZONE_OFFSET)
+ fromCal.get(Calendar.DST_OFFSET);
Calendar toCal = Calendar.getInstance(toTz);
toCal.setTime(tstamp);
int toOffset =
toCal.get(Calendar.ZONE_OFFSET) + toCal.get(Calendar.DST_OFFSET);
int offsetDiff = toOffset - fromOffset;
long toTime = toCal.getTime().getTime();
toTime += offsetDiff;
Timestamp changedTimestamp = new Timestamp(toTime);
return changedTimestamp;
*/
return tstamp;
}
public static Time changeTimezone(Time t, TimeZone fromTz, TimeZone toTz)
{
/*
// Convert the timestamp to the GMT timezone
Calendar fromCal = Calendar.getInstance(fromTz);
fromCal.setTime(t);
int fromOffset =
fromCal.get(Calendar.ZONE_OFFSET)
+ fromCal.get(Calendar.DST_OFFSET);
Calendar toCal = Calendar.getInstance(toTz);
toCal.setTime(t);
int toOffset =
toCal.get(Calendar.ZONE_OFFSET) + toCal.get(Calendar.DST_OFFSET);
int offsetDiff = toOffset - fromOffset;
long toTime = toCal.getTime().getTime();
toTime += offsetDiff;
Time changedTime = new Time(toTime);
return changedTime;
*/
return t;
}
}
TimeZone.getTimeZone("America/New_York")
import java.util.Calendar;
import java.util.TimeZone;
public class Main {
public static void main(String[] args) {
Calendar calNewYork = Calendar.getInstance();
calNewYork.setTimeZone(TimeZone.getTimeZone("America/New_York"));
System.out.println("Time in New York: " + calNewYork.get(Calendar.HOUR_OF_DAY) + ":"
+ calNewYork.get(Calendar.MINUTE));
}
}
//Time in New York: 11:51
TimeZone.getTimeZone("Asia/Tokyo")
import java.util.Calendar;
import java.util.TimeZone;
public class Main {
public static void main(String[] args) {
Calendar calTokyo = Calendar.getInstance();
calTokyo.setTimeZone(TimeZone.getTimeZone("Asia/Tokyo"));
System.out.println("Time in Tokyo: " + calTokyo.get(Calendar.HOUR_OF_DAY) + ":"
+ calTokyo.get(Calendar.MINUTE));
}
}
//Time in Tokyo: 1:51
TimeZone.getTimeZone("Europe/Paris")
import java.util.Calendar;
import java.util.TimeZone;
public class Main {
public static void main(String[] args) {
Calendar calParis = Calendar.getInstance();
calParis.setTimeZone(TimeZone.getTimeZone("Europe/Paris"));
System.out.println("Time in Paris: " + calParis.get(Calendar.HOUR_OF_DAY) + ":"
+ calParis.get(Calendar.MINUTE));
}
}
//Time in Paris: 17:51
To obtain a reference to a TimeZone object corresponding to a given time zone ID
import java.util.GregorianCalendar;
import java.util.TimeZone;
public class MainClass {
public static void main(String[] a) {
GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("America/Chicago"));
System.out.println(calendar);
}
}
java.util.GregorianCalendar[ time=1168970797636, areFieldsSet=true, areAllFieldsSet=true, lenient=true, zone=sun.util.calendar.ZoneInfo[id="America/Chicago", offset=-21600000, dstSavings=3600000, useDaylight=true, transitions=235, lastRule=java.util.SimpleTimeZone[id=America/Chicago, offset=-21600000, dstSavings=3600000, useDaylight=true, startYear=0, startMode=3, startMonth=3, startDay=1, startDayOfWeek=1, startTime=7200000, startTimeMode=0, endMode=2, endMonth=9, endDay=-1, endDayOfWeek=1, endTime=7200000, endTimeMode=0] ], firstDayOfWeek=1, minimalDaysInFirstWeek=1, ERA=1, YEAR=2007, MONTH=0, WEEK_OF_YEAR=3, WEEK_OF_MONTH=3, DAY_OF_MONTH=16, DAY_OF_YEAR=16, DAY_OF_WEEK=3, DAY_OF_WEEK_IN_MONTH=3, AM_PM=1, HOUR=0, HOUR_OF_DAY=12, MINUTE=6, SECOND=37, MILLISECOND=636, ZONE_OFFSET=-21600000, DST_OFFSET=0]