Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get abbreviation from Java TimeZone?

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
like image 665
sugeesh Avatar asked May 16 '19 11:05

sugeesh


People also ask

What is TimeZone abbreviation?

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.

How do I get JVM TimeZone?

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.

What is UTC TimeZone in java?

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.


2 Answers

Looks like you are looking for this:

String s = TimeZone.getTimeZone("America/Los_Angeles")
                   .getDisplayName(false, TimeZone.SHORT);
like image 72
Eugene Avatar answered Nov 15 '22 17:11

Eugene


java.time

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.

like image 20
Ole V.V. Avatar answered Nov 15 '22 17:11

Ole V.V.