You can use a java.time.ZoneId
to parse them and display some corresponding short name:
public static void main(String[] args) throws Exception {
// example String zones
String zuerich = "Europe/Zurich";
String antananarivo = "Indian/Antananarivo";
// create ZoneIds from the Strings
ZoneId zueri = ZoneId.of(zuerich);
ZoneId antan = ZoneId.of(antananarivo);
// print their short names / abbreviations
System.out.println(zueri.getDisplayName(TextStyle.SHORT, Locale.ENGLISH));
System.out.println(antan.getDisplayName(TextStyle.SHORT, Locale.ENGLISH));
}
Output:
CET
EAT
NOTE that this CET
is probably not quite correct due to it being CEST
at the moment.
EDIT
You can have the GMT representations if you take some instant into account:
public static void main(String[] args) throws Exception {
// example String zones
String zuerich = "Europe/Zurich";
String antananarivo = "Indian/Antananarivo";
// create ZoneIds from the Strings
ZoneId zueri = ZoneId.of(zuerich);
ZoneId antan = ZoneId.of(antananarivo);
// create a formatter that outputs the GMT+/-XX:XX representations
DateTimeFormatter gmtFormatter = DateTimeFormatter.ofPattern("OOOO");
// or take "now" as a temporal reference and print the GMT representation per zone
ZonedDateTime nowInZurich = ZonedDateTime.now(zueri);
ZonedDateTime nowInAntananarivo = ZonedDateTime.now(antan);
System.out.println(nowInZurich.format(gmtFormatter));
System.out.println(nowInAntananarivo.format(gmtFormatter));
// find out about the difference when the time switches from daylight saving
ZonedDateTime sixMonthsLaterInZurich = nowInZurich.plusMonths(6);
ZonedDateTime sixMonthsLaterInAntananarivo = nowInAntananarivo.plusMonths(6);
System.out.println(sixMonthsLaterInZurich.format(gmtFormatter));
System.out.println(sixMonthsLaterInAntananarivo.format(gmtFormatter));
}
prints
GMT+02:00
GMT+03:00
GMT+01:00
GMT+03:00
Looks like Zürich is switching an hour six months after now (July 16. 2021) but Antananarivo doesn't.
CST
could meanCentral Standard Time
,China Standard Time
, orCuba Standard Time
, which are 3 entirely different time zones.