TimeZone.getTimeZone("PST") vs TimeZone.getTimeZone("America/Los_Angeles")
Asked Answered
E

4

5

I'm Using Java 8,

Earlier in our code, We were using sdf.setTimeZone(TimeZone.getTimeZone("PDT")); to convert to US Pacific which was failed(not throwing any errors but converted to default timezone) due to PDT is not a valid ZoneId.

So I look for setTimeZone(TimeZone.getTimeZone("PST")); which is also not available in the TimeZone.getAvailableIDs() values.

Finally I end up with using sdf.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));

Now, One of our friends using setTimeZone(TimeZone.getTimeZone("PST")); to convert to us-pacific timezone and the conversion is happening properly..

Question is,

What is the difference between TimeZone.getTimeZone("PST"); and TimeZone.getTimeZone("America/Los_Angeles");

Which one is better to use ?

Eterne answered 11/4, 2019 at 8:6 Comment(1)
I recommend you neither use SimpleDateFormat nor TimeZone. Both classes are poorly designed and long outdated (the former in particular notoriously troublesome). Instead use DateTimeFormatter and ZoneId, both from java.time, the modern Java date and time API, built into your Java 8 (and later).Dripping
T
12

Quoting the documentation for Error Prone's ThreeLetterTimeZoneID check:

According to the Javadoc of java.util.TimeZone:

For compatibility with JDK 1.1.x, some other three-letter time zone IDs (such as “PST”, “CTT”, “AST”) are also supported. However, their use is deprecated because the same abbreviation is often used for multiple time zones (for example, “CST” could be U.S. “Central Standard Time” and “China Standard Time”), and the Java platform can then only recognize one of them.

Aside from the ambiguity between timezones, there is inconsistency in the observance of Daylight Savings Time for the returned time zone, meaning the TimeZone obtained may not be what you expect. Examples include:

DateTime.getTimeZone("PST") does observe daylight savings time; however, the identifier implies that it is Pacific Standard Time, i.e. daylight savings time is not observed. DateTime.getTimeZone("EST") (and "MST" and "HST") do not observe daylight savings time. However, this is inconsistent with PST (and others), so you may believe that daylight savings time will be observed.

So: Use the full America/Los_Angeles format to minimize the ambiguity in the code.

Trilingual answered 11/4, 2019 at 8:36 Comment(0)
O
4

According to the Java 8 Timezone documentation, PST use is deprecated because the same abbreviation is often used for multiple time zones. That's why is preferred to use America/Los_Angeles

Offend answered 11/4, 2019 at 8:22 Comment(0)
C
2

Better is to use "America/Los_Angeles" as this is valid time zone according to TZ Database. See this LINK.

For compatibility with JDK 1.1.x, some other three-letter time zone IDs (such as "PST", "CTT", "AST") are also supported. However, their use is deprecated because the same abbreviation is often used for multiple time zones (for example, "CST" could be U.S. "Central Standard Time" and "China Standard Time"), and the Java platform can then only recognize one of them..

See this LINK

You can also see THIS SO post that shows problem with using 3 letter timezone ID.

Chinookan answered 11/4, 2019 at 8:28 Comment(0)
D
1

TL;DR Don’t use PST. Use America/Los_Angeles. Also don’t use the TimeZone class. Use ZoneId.

  • PST may mean Pitcairn Standard Time, Pacific Standard Time or Philippine Standard Time.
  • If you take PST to mean Pacific Standard Time, that is not a time zone since all of the places that use it as their standard time are currently and for most of the year on Pacific Daylight Time instead.
  • While the outdated TimeZone class interprets PST the same as America/Los_Angeles, as others have long said, the three letter abbreviations are deprecated. Should TimeZone one day (however unlikely) cease to recognize PST, it will instead give you GMT, which is certainly not what you want.

java.time

The TimeZone class was poorly designed and sometimes confusing, and is fortunately long outdated, replaced by ZoneId from java.time, the modern Java date and time API, 5 years ago.

A short code example to help you get started using the modern API:

    DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL)
            .withLocale(Locale.US);
    ZonedDateTime dateTime = ZonedDateTime.now(ZoneId.of("America/Los_Angeles"));
    System.out.println(dateTime.format(formatter));

Friday, April 12, 2019 at 8:14:09 AM Pacific Daylight Time

One advantage of ZoneId is it doesn’t readily let you use PST as a time zone: ZoneId.of("PST") throws a java.time.zone.ZoneRulesException: Unknown time-zone ID: PST (there is a workaround if you insist, but as we have said, you shouldn’t).

Links

Dripping answered 12/4, 2019 at 15:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.