Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

timezone

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

like image 950
nr1 Avatar asked Oct 14 '22 10:10

nr1


1 Answers

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,
like image 150
mob Avatar answered Oct 27 '22 17:10

mob