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

15,150

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


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:

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,
Share:
15,150
nr1
Author by

nr1

Updated on June 21, 2022

Comments

  • nr1
    nr1 about 2 years

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