0

In my Java code I have appointments that have time zones related to each one.

The timezones are in the following format:

Europe/Zurich,
Indian/Antananarivo

I want to convert these to different formats. How can I convert these time zones into for example:

GMT
EST
5
  • 4
    You can't if you don't also take the actual date/time into account. For Zurich, for example, the current timezone is CEST (Central European Summer Time). It will switch back to CET (Central European Time) in winter. Commented Jul 16, 2021 at 9:38
  • Is there anyway I can do that? or a library already? Commented Jul 16, 2021 at 9:42
  • Please don't use code font for non-code aspects of your question - it's really distracting.
    – Jon Skeet
    Commented Jul 16, 2021 at 10:12
  • 2
    Those are incomplete, e.g. CST could mean Central Standard Time, China Standard Time, or Cuba Standard Time, which are 3 entirely different time zones.
    – Andreas
    Commented Jul 16, 2021 at 10:12
  • Why do you need the 3-letter name for the timezone? The 3-letter timezone names are error-prone. All you should care about is the correct date-time which you can process using the unambiguous standard naming pattern, Region/City e.g. Europe/Zurich, and not about the 3-letter ambiguous names. If your requirement is to just get the 3-letter names that you have mentioned, Java (or probably any language) does not have a definitive answer. However, if you need some help regarding the processing of the date-time, edit the question so that someone can help you. Commented Jul 17, 2021 at 12:49

1 Answer 1

1

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.

5
  • Thanks, will this also be accurate for GMT/BST etc? Commented Jul 16, 2021 at 9:51
  • What's BST? Could you explain, please?
    – deHaar
    Commented Jul 16, 2021 at 9:56
  • British summer time vs GMT , it is related to daylight savings Commented Jul 16, 2021 at 9:57
  • These will become related to daylight savings as soon as you make it depend on some instant, see my edit. However, I have no idea how to create BST.
    – deHaar
    Commented Jul 16, 2021 at 9:58
  • @java12399900 Really no idea about BST.
    – deHaar
    Commented Jul 16, 2021 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.