我写了以下方法:
private static String getDate(final long addToCurrentMillis, final String pattern) {
final long timeMilliseconds = System.currentTimeMillis() + addToCurrentMillis;
final SimpleDateFormat sdf = new SimpleDateFormat(pattern);
final Date resultdate = new Date(timeMilliseconds);
final TimeZone timeZone = TimeZone.getDefault();
sdf.setTimeZone(timeZone);
return sdf.format(resultdate);
}
我使用了"yyyy-MM-dd zzz"
模式,结果是:
2015-10-24 EEST
我正在使用此方法进行单元测试。问题是我尝试将此字符串转换回我想要测试的代码中的TimeZone
,但TimeZone
并不了解EEST时区。有什么想法吗?
答案 0 :(得分:6)
TimeZone.getAvailableIDs不包含EEST
它包含time zone IDs from IANA Time Zone Database,而不是abbreviated time zone names。
要找出每个时区ID的缩写时区名称,请在下面的代码段中运行:
SimpleDateFormat sdf = new SimpleDateFormat("zzz");
Date now = new Date();
for (String id : TimeZone.getAvailableIDs()) {
sdf.setTimeZone(TimeZone.getTimeZone(id));
System.out.println(id + " = " + sdf.format(now));
}
在我当前的机器上(Ubuntu Mint 17.2,JDK 1.8.0_60),输出如下:
Africa/Abidjan = GMT
Africa/Accra = GMT
Africa/Addis_Ababa = EAT
Africa/Algiers = CET
Africa/Asmara = EAT
Africa/Asmera = EAT
Africa/Bamako = GMT
Africa/Bangui = WAT
Africa/Banjul = GMT
Africa/Bissau = GMT
Africa/Blantyre = CAT
Africa/Brazzaville = WAT
Africa/Bujumbura = CAT
Africa/Cairo = EET
Africa/Casablanca = WET
Africa/Ceuta = CET
Africa/Conakry = GMT
Africa/Dakar = GMT
Africa/Dar_es_Salaam = EAT
Africa/Djibouti = EAT
Africa/Douala = WAT
Africa/El_Aaiun = WET
Africa/Freetown = GMT
Africa/Gaborone = CAT
Africa/Harare = CAT
Africa/Johannesburg = SAST
Africa/Juba = EAT
Africa/Kampala = EAT
Africa/Khartoum = EAT
Africa/Kigali = CAT
Africa/Kinshasa = WAT
Africa/Lagos = WAT
Africa/Libreville = WAT
Africa/Lome = GMT
Africa/Luanda = WAT
Africa/Lubumbashi = CAT
Africa/Lusaka = CAT
Africa/Malabo = WAT
Africa/Maputo = CAT
Africa/Maseru = SAST
Africa/Mbabane = SAST
Africa/Mogadishu = EAT
Africa/Monrovia = GMT
Africa/Nairobi = EAT
Africa/Ndjamena = WAT
Africa/Niamey = WAT
Africa/Nouakchott = GMT
Africa/Ouagadougou = GMT
Africa/Porto-Novo = WAT
Africa/Sao_Tome = GMT
Africa/Timbuktu = GMT
Africa/Tripoli = EET
Africa/Tunis = CET
Africa/Windhoek = WAST
America/Adak = HDT
America/Anchorage = AKDT
America/Anguilla = AST
America/Antigua = AST
America/Araguaina = BRT
America/Argentina/Buenos_Aires = ART
America/Argentina/Catamarca = ART
America/Argentina/ComodRivadavia = ART
America/Argentina/Cordoba = ART
America/Argentina/Jujuy = ART
America/Argentina/La_Rioja = ART
America/Argentina/Mendoza = ART
America/Argentina/Rio_Gallegos = ART
America/Argentina/Salta = ART
America/Argentina/San_Juan = ART
America/Argentina/San_Luis = ART
America/Argentina/Tucuman = ART
America/Argentina/Ushuaia = ART
America/Aruba = AST
America/Asuncion = PYST
America/Atikokan = EST
America/Atka = HDT
America/Bahia = BRT
America/Bahia_Banderas = CST
America/Barbados = AST
America/Belem = BRT
America/Belize = CST
America/Blanc-Sablon = AST
America/Boa_Vista = AMT
America/Bogota = COT
America/Boise = MDT
America/Buenos_Aires = ART
America/Cambridge_Bay = MDT
America/Campo_Grande = AMST
America/Cancun = EST
America/Caracas = VET
America/Catamarca = ART
America/Cayenne = GFT
America/Cayman = EST
America/Chicago = CDT
America/Chihuahua = MST
America/Coral_Harbour = EST
America/Cordoba = ART
America/Costa_Rica = CST
America/Creston = MST
America/Cuiaba = AMST
America/Curacao = AST
America/Danmarkshavn = GMT
America/Dawson = PDT
America/Dawson_Creek = MST
America/Denver = MDT
America/Detroit = EDT
America/Dominica = AST
America/Edmonton = MDT
America/Eirunepe = ACT
America/El_Salvador = CST
America/Ensenada = PDT
America/Fort_Wayne = EDT
America/Fortaleza = BRT
America/Glace_Bay = ADT
America/Godthab = WGT
America/Goose_Bay = ADT
America/Grand_Turk = ADT
America/Grenada = AST
America/Guadeloupe = AST
America/Guatemala = CST
America/Guayaquil = ECT
America/Guyana = GYT
America/Halifax = ADT
America/Havana = CDT
America/Hermosillo = MST
America/Indiana/Indianapolis = EDT
America/Indiana/Knox = CDT
America/Indiana/Marengo = EDT
America/Indiana/Petersburg = EDT
America/Indiana/Tell_City = CDT
America/Indiana/Vevay = EDT
America/Indiana/Vincennes = EDT
America/Indiana/Winamac = EDT
America/Indianapolis = EDT
America/Inuvik = MDT
America/Iqaluit = EDT
America/Jamaica = EST
America/Jujuy = ART
America/Juneau = AKDT
America/Kentucky/Louisville = EDT
America/Kentucky/Monticello = EDT
America/Knox_IN = CDT
America/Kralendijk = AST
America/La_Paz = BOT
America/Lima = PET
America/Los_Angeles = PDT
America/Louisville = EDT
America/Lower_Princes = AST
America/Maceio = BRT
America/Managua = CST
America/Manaus = AMT
America/Marigot = AST
America/Martinique = AST
America/Matamoros = CDT
America/Mazatlan = MST
America/Mendoza = ART
America/Menominee = CDT
America/Merida = CST
America/Metlakatla = PST
America/Mexico_City = CST
America/Miquelon = PMDT
America/Moncton = ADT
America/Monterrey = CST
America/Montevideo = UYST
America/Montreal = EDT
America/Montserrat = AST
America/Nassau = EDT
America/New_York = EDT
America/Nipigon = EDT
America/Nome = AKDT
America/Noronha = FNT
America/North_Dakota/Beulah = CDT
America/North_Dakota/Center = CDT
America/North_Dakota/New_Salem = CDT
America/Ojinaga = MDT
America/Panama = EST
America/Pangnirtung = EDT
America/Paramaribo = SRT
America/Phoenix = MST
America/Port-au-Prince = EDT
America/Port_of_Spain = AST
America/Porto_Acre = ACT
America/Porto_Velho = AMT
America/Puerto_Rico = AST
America/Rainy_River = CDT
America/Rankin_Inlet = CDT
America/Recife = BRT
America/Regina = CST
America/Resolute = CDT
America/Rio_Branco = ACT
America/Rosario = ART
America/Santa_Isabel = PST
America/Santarem = BRT
America/Santiago = CLT
America/Santo_Domingo = AST
America/Sao_Paulo = BRST
America/Scoresbysund = EGT
America/Shiprock = MDT
America/Sitka = AKDT
America/St_Barthelemy = AST
America/St_Johns = NDT
America/St_Kitts = AST
America/St_Lucia = AST
America/St_Thomas = AST
America/St_Vincent = AST
America/Swift_Current = CST
America/Tegucigalpa = CST
America/Thule = ADT
America/Thunder_Bay = EDT
America/Tijuana = PDT
America/Toronto = EDT
America/Tortola = AST
America/Vancouver = PDT
America/Virgin = AST
America/Whitehorse = PDT
America/Winnipeg = CDT
America/Yakutat = AKDT
America/Yellowknife = MDT
Antarctica/Casey = AWST
Antarctica/Davis = DAVT
Antarctica/DumontDUrville = DDUT
Antarctica/Macquarie = MIST
Antarctica/Mawson = MAWT
Antarctica/McMurdo = NZDT
Antarctica/Palmer = CLT
Antarctica/Rothera = ROTT
Antarctica/South_Pole = NZDT
Antarctica/Syowa = SYOT
Antarctica/Troll = UTC
Antarctica/Vostok = VOST
Arctic/Longyearbyen = CET
Asia/Aden = AST
Asia/Almaty = ALMT
Asia/Amman = EEST
Asia/Anadyr = ANAT
Asia/Aqtau = AQTT
Asia/Aqtobe = AQTT
Asia/Ashgabat = TMT
Asia/Ashkhabad = TMT
Asia/Baghdad = AST
Asia/Bahrain = AST
Asia/Baku = AZT
Asia/Bangkok = ICT
Asia/Beirut = EET
Asia/Bishkek = KGT
Asia/Brunei = BNT
Asia/Calcutta = IST
Asia/Chita = IRKT
Asia/Choibalsan = CHOT
Asia/Chongqing = CST
Asia/Chungking = CST
Asia/Colombo = IST
Asia/Dacca = BDT
Asia/Damascus = EEST
Asia/Dhaka = BDT
Asia/Dili = TLT
Asia/Dubai = GST
Asia/Dushanbe = TJT
Asia/Gaza = EET
Asia/Harbin = CST
Asia/Hebron = EET
Asia/Ho_Chi_Minh = ICT
Asia/Hong_Kong = HKT
Asia/Hovd = HOVT
Asia/Irkutsk = IRKT
Asia/Istanbul = EET
Asia/Jakarta = WIB
Asia/Jayapura = WIT
Asia/Jerusalem = IST
Asia/Kabul = AFT
Asia/Kamchatka = PETT
Asia/Karachi = PKT
Asia/Kashgar = XJT
Asia/Kathmandu = NPT
Asia/Katmandu = NPT
Asia/Khandyga = YAKT
Asia/Kolkata = IST
Asia/Krasnoyarsk = KRAT
Asia/Kuala_Lumpur = MYT
Asia/Kuching = MYT
Asia/Kuwait = AST
Asia/Macao = CST
Asia/Macau = CST
Asia/Magadan = MAGT
Asia/Makassar = WITA
Asia/Manila = PHT
Asia/Muscat = GST
Asia/Nicosia = EET
Asia/Novokuznetsk = KRAT
Asia/Novosibirsk = NOVT
Asia/Omsk = OMST
Asia/Oral = ORAT
Asia/Phnom_Penh = ICT
Asia/Pontianak = WIB
Asia/Pyongyang = KST
Asia/Qatar = AST
Asia/Qyzylorda = QYZT
Asia/Rangoon = MMT
Asia/Riyadh = AST
Asia/Saigon = ICT
Asia/Sakhalin = SAKT
Asia/Samarkand = UZT
Asia/Seoul = KST
Asia/Shanghai = CST
Asia/Singapore = SGT
Asia/Srednekolymsk = SRET
Asia/Taipei = CST
Asia/Tashkent = UZT
Asia/Tbilisi = GET
Asia/Tehran = IRST
Asia/Tel_Aviv = IST
Asia/Thimbu = BTT
Asia/Thimphu = BTT
Asia/Tokyo = JST
Asia/Ujung_Pandang = WITA
Asia/Ulaanbaatar = ULAT
Asia/Ulan_Bator = ULAT
Asia/Urumqi = XJT
Asia/Ust-Nera = VLAT
Asia/Vientiane = ICT
Asia/Vladivostok = VLAT
Asia/Yakutsk = YAKT
Asia/Yekaterinburg = YEKT
Asia/Yerevan = AMT
Atlantic/Azores = AZOT
Atlantic/Bermuda = ADT
Atlantic/Canary = WET
Atlantic/Cape_Verde = CVT
Atlantic/Faeroe = WET
Atlantic/Faroe = WET
Atlantic/Jan_Mayen = CET
Atlantic/Madeira = WET
Atlantic/Reykjavik = GMT
Atlantic/South_Georgia = GST
Atlantic/St_Helena = GMT
Atlantic/Stanley = FKT
Australia/ACT = AEDT
Australia/Adelaide = ACDT
Australia/Brisbane = AEST
Australia/Broken_Hill = ACDT
Australia/Canberra = AEDT
Australia/Currie = AEDT
Australia/Darwin = ACST
Australia/Eucla = ACWST
Australia/Hobart = AEDT
Australia/LHI = LHDT
Australia/Lindeman = AEST
Australia/Lord_Howe = LHDT
Australia/Melbourne = AEDT
Australia/NSW = AEDT
Australia/North = ACST
Australia/Perth = AWST
Australia/Queensland = AEST
Australia/South = ACDT
Australia/Sydney = AEDT
Australia/Tasmania = AEDT
Australia/Victoria = AEDT
Australia/West = AWST
Australia/Yancowinna = ACDT
Brazil/Acre = ACT
Brazil/DeNoronha = FNT
Brazil/East = BRST
Brazil/West = AMT
CET = CET
CST6CDT = CDT
Canada/Atlantic = ADT
Canada/Central = CDT
Canada/East-Saskatchewan = CST
Canada/Eastern = EDT
Canada/Mountain = MDT
Canada/Newfoundland = NDT
Canada/Pacific = PDT
Canada/Saskatchewan = CST
Canada/Yukon = PDT
Chile/Continental = CLT
Chile/EasterIsland = EAST
Cuba = CDT
EET = EET
EST5EDT = EDT
Egypt = EET
Eire = GMT
Etc/GMT = GMT
Etc/GMT+0 = GMT
Etc/GMT+1 = GMT-01:00
Etc/GMT+10 = GMT-10:00
Etc/GMT+11 = GMT-11:00
Etc/GMT+12 = GMT-12:00
Etc/GMT+2 = GMT-02:00
Etc/GMT+3 = GMT-03:00
Etc/GMT+4 = GMT-04:00
Etc/GMT+5 = GMT-05:00
Etc/GMT+6 = GMT-06:00
Etc/GMT+7 = GMT-07:00
Etc/GMT+8 = GMT-08:00
Etc/GMT+9 = GMT-09:00
Etc/GMT-0 = GMT
Etc/GMT-1 = GMT+01:00
Etc/GMT-10 = GMT+10:00
Etc/GMT-11 = GMT+11:00
Etc/GMT-12 = GMT+12:00
Etc/GMT-13 = GMT+13:00
Etc/GMT-14 = GMT+14:00
Etc/GMT-2 = GMT+02:00
Etc/GMT-3 = GMT+03:00
Etc/GMT-4 = GMT+04:00
Etc/GMT-5 = GMT+05:00
Etc/GMT-6 = GMT+06:00
Etc/GMT-7 = GMT+07:00
Etc/GMT-8 = GMT+08:00
Etc/GMT-9 = GMT+09:00
Etc/GMT0 = GMT
Etc/Greenwich = GMT
Etc/UCT = UTC
Etc/UTC = UTC
Etc/Universal = UTC
Etc/Zulu = UTC
Europe/Amsterdam = CET
Europe/Andorra = CET
Europe/Athens = EET
Europe/Belfast = GMT
Europe/Belgrade = CET
Europe/Berlin = CET
Europe/Bratislava = CET
Europe/Brussels = CET
Europe/Bucharest = EET
Europe/Budapest = CET
Europe/Busingen = CET
Europe/Chisinau = EET
Europe/Copenhagen = CET
Europe/Dublin = GMT
Europe/Gibraltar = CET
Europe/Guernsey = GMT
Europe/Helsinki = EET
Europe/Isle_of_Man = GMT
Europe/Istanbul = EET
Europe/Jersey = GMT
Europe/Kaliningrad = EET
Europe/Kiev = EET
Europe/Lisbon = WET
Europe/Ljubljana = CET
Europe/London = GMT
Europe/Luxembourg = CET
Europe/Madrid = CET
Europe/Malta = CET
Europe/Mariehamn = EET
Europe/Minsk = MSK
Europe/Monaco = CET
Europe/Moscow = MSK
Europe/Nicosia = EET
Europe/Oslo = CET
Europe/Paris = CET
Europe/Podgorica = CET
Europe/Prague = CET
Europe/Riga = EET
Europe/Rome = CET
Europe/Samara = SAMT
Europe/San_Marino = CET
Europe/Sarajevo = CET
Europe/Simferopol = MSK
Europe/Skopje = CET
Europe/Sofia = EET
Europe/Stockholm = CET
Europe/Tallinn = EET
Europe/Tirane = CET
Europe/Tiraspol = EET
Europe/Uzhgorod = EET
Europe/Vaduz = CET
Europe/Vatican = CET
Europe/Vienna = CET
Europe/Vilnius = EET
Europe/Volgograd = MSK
Europe/Warsaw = CET
Europe/Zagreb = CET
Europe/Zaporozhye = EET
Europe/Zurich = CET
GB = GMT
GB-Eire = GMT
GMT = GMT
GMT0 = GMT
Greenwich = GMT
Hongkong = HKT
Iceland = GMT
Indian/Antananarivo = EAT
Indian/Chagos = IOT
Indian/Christmas = CXT
Indian/Cocos = CCT
Indian/Comoro = EAT
Indian/Kerguelen = TFT
Indian/Mahe = SCT
Indian/Maldives = MVT
Indian/Mauritius = MUT
Indian/Mayotte = EAT
Indian/Reunion = RET
Iran = IRST
Israel = IST
Jamaica = EST
Japan = JST
Kwajalein = MHT
Libya = EET
MET = MET
MST7MDT = MDT
Mexico/BajaNorte = PDT
Mexico/BajaSur = MST
Mexico/General = CST
NZ = NZDT
NZ-CHAT = CHADT
Navajo = MDT
PRC = CST
PST8PDT = PDT
Pacific/Apia = WSDT
Pacific/Auckland = NZDT
Pacific/Bougainville = BST
Pacific/Chatham = CHADT
Pacific/Chuuk = CHUT
Pacific/Easter = EAST
Pacific/Efate = VUT
Pacific/Enderbury = PHOT
Pacific/Fakaofo = TKT
Pacific/Fiji = FJT
Pacific/Funafuti = TVT
Pacific/Galapagos = GALT
Pacific/Gambier = GAMT
Pacific/Guadalcanal = SBT
Pacific/Guam = ChST
Pacific/Honolulu = HST
Pacific/Johnston = HST
Pacific/Kiritimati = LINT
Pacific/Kosrae = KOST
Pacific/Kwajalein = MHT
Pacific/Majuro = MHT
Pacific/Marquesas = MART
Pacific/Midway = SST
Pacific/Nauru = NRT
Pacific/Niue = NUT
Pacific/Norfolk = NFT
Pacific/Noumea = NCT
Pacific/Pago_Pago = SST
Pacific/Palau = PWT
Pacific/Pitcairn = PST
Pacific/Pohnpei = PONT
Pacific/Ponape = PONT
Pacific/Port_Moresby = PGT
Pacific/Rarotonga = CKT
Pacific/Saipan = ChST
Pacific/Samoa = SST
Pacific/Tahiti = TAHT
Pacific/Tarawa = GILT
Pacific/Tongatapu = TOT
Pacific/Truk = CHUT
Pacific/Wake = WAKT
Pacific/Wallis = WFT
Pacific/Yap = CHUT
Poland = CET
Portugal = WET
ROK = KST
Singapore = SGT
SystemV/AST4 = AST
SystemV/AST4ADT = AST
SystemV/CST6 = CST
SystemV/CST6CDT = CST
SystemV/EST5 = EST
SystemV/EST5EDT = EST
SystemV/HST10 = HST
SystemV/MST7 = MST
SystemV/MST7MDT = MST
SystemV/PST8 = PST
SystemV/PST8PDT = PST
SystemV/YST9 = AKST
SystemV/YST9YDT = AKST
Turkey = EET
UCT = UTC
US/Alaska = AKDT
US/Aleutian = HDT
US/Arizona = MST
US/Central = CDT
US/East-Indiana = EDT
US/Eastern = EDT
US/Hawaii = HST
US/Indiana-Starke = CDT
US/Michigan = EDT
US/Mountain = MDT
US/Pacific = PDT
US/Pacific-New = PDT
US/Samoa = SST
UTC = UTC
Universal = UTC
W-SU = MSK
WET = WET
Zulu = UTC
EST = EST
HST = HST
MST = MST
ACT = ACST
AET = AEDT
AGT = ART
ART = EET
AST = AKDT
BET = BRST
BST = BDT
CAT = CAT
CNT = NDT
CST = CDT
CTT = CST
EAT = EAT
ECT = CET
IET = EDT
IST = IST
JST = JST
MIT = WSDT
NET = AMT
NST = NZDT
PLT = PKT
PNT = MST
PRT = AST
PST = PDT
SST = SBT
VST = ICT
要确定默认时区的ID,只需执行以下操作:
System.out.println(TimeZone.getDefault().getID());