Java:将“欧洲/伦敦”转换为 3 位时区

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2194119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:59:02  来源:igfitidea点击:

Java: Convert "Europe/London" to 3 digit timezone

javatimezone

提问by nr1

How can I convert the timezone identifiers to the corresponding 3 digit string? For example "Europe/London" => "GMT"

如何将时区标识符转换为相应的 3 位字符串?例如“欧洲/伦敦”=>“格林威治标准时间”

回答by mob

See the String getDisplayName(boolean daylight,int style)method in java.util.TimeZone. The style may be TimeZone.LONGor TimeZone.SHORTwith the short style returning the short name of the time zone.

参见 中的String getDisplayName(boolean daylight,int style)方法java.util.TimeZone。样式可以是TimeZone.LONGTimeZone.SHORT带有短样式返回时区的短名称。



A more long winded approach is to check the output of String[] TimeZone.getAvailableIDs(int offset). The short time zone codes can be ambiguous or redundant, so maybe you might want to be more thorough about it:

更冗长的方法是检查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,

回答by dirtyhandsphp

You can use following code to find 3 digit abbreviation for any timezone.

您可以使用以下代码查找任何时区的 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);