I need to get the Abbreviation(as String) for the existing Java TimeZone Object. Is there any way to do this without direct mapping ?
Eg- TimeZone - ("America/Los_Angeles")
Abbreviation - PDT
Time zones are often represented by alphabetic abbreviations such as "EST", "WST", and "CST", but these are not part of the international time and date standard ISO 8601 and their use as sole designator for a time zone is discouraged.
By default, the JVM reads time zone information from the operating system and stores it in the TimeZone class. To get the default time zone set in the JVM using the method TimeZone. getDefault() . To get the list of all supported timezones, use the method TimeZone.
Java For Testers UTC stands for Co-ordinated Universal Time. It is time standard and is commonly used across the world. All timezones are computed comparatively with UTC as offset.
Looks like you are looking for this:
String s = TimeZone.getTimeZone("America/Los_Angeles")
.getDisplayName(false, TimeZone.SHORT);
I suggest that the abbreviation you want for America/Los_Angeles
would be PT
for Pacific Time. This name and this abbreviation can be used all year regardless of summer time (DST). By contrast PDT
is for Pacific Daylight Time so is used during summer only.
ZoneId zone = ZoneId.of("America/Los_Angeles");
System.out.println("Summer time neutral abbreviation: "
+ zone.getDisplayName(TextStyle.SHORT_STANDALONE, Locale.ENGLISH));
Output:
Summer time neutral abbreviation: PT
If you want the abbreviation relevant for this part of year only (standard time or summer time):
DateTimeFormatter zoneAbbreviationFormatter
= DateTimeFormatter.ofPattern("zzz", Locale.ENGLISH);
System.out.println("Current abbreviation for either standard or summer time: "
+ ZonedDateTime.now(zone).format(zoneAbbreviationFormatter));
Output when running in May:
Current abbreviation for either standard or summer time: PDT
Please substitute your desired locale. For many locales it will not make any difference, but some time zones have localized abbreviations for some locales.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With