如何将时区标识符转换为相应的3位数字符串? 例如“Europe / London”=> “GMT”
答案 0 :(得分:2)
请参阅String getDisplayName(boolean daylight,int style)
中的java.util.TimeZone
方法。样式可以是TimeZone.LONG
或TimeZone.SHORT
,短样式会返回时区的短名称。
<小时/>
更长时间的方法是检查String[] TimeZone.getAvailableIDs(int offset)
的输出。短时区代码可能不明确或冗余,因此您可能希望对此更加透彻:
TimeZone tz = TimeZone.getTimeZone("Europe/London");
for (String s : TimeZone.getAvailableIDs(tz.getOffset(System.currentTimeMillis()))) {
System.out.print(s + ",");
}
------------------------------------------------------
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/Faroe,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/Guernsey,Europe/Isle_of_Man,Europe/Jersey,Europe/Lisbon,
Europe/London,GB,GB-Eire,GMT,GMT0,Greenwich,Iceland,Portugal,UCT,UTC,
Universal,WET,Zulu,
答案 1 :(得分:0)
您可以使用以下代码查找任何时区的3位数缩写。
Date date = new Date();
String TimeZoneIds[] = TimeZone.getAvailableIDs();
String timezoneShortName = "";
String timezoneLongName = "Europe/London";
for (int i = 0; i < TimeZoneIds.length; i++) {
TimeZone tz = TimeZone.getTimeZone(TimeZoneIds[i]);
String tzName = tz.getDisplayName(tz.inDaylightTime(date),TimeZone.SHORT);
if(timezoneLongName.equals(TimeZoneIds[i])){
timezoneShortName = tzName;
break;
}
}
System.out.println(timezoneShortName);