Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TimeZone.getTimeZone("PST") vs TimeZone.getTimeZone("America/Los_Angeles")

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 ?

like image 379
PrabaharanKathiresan Avatar asked Apr 11 '19 08:04

PrabaharanKathiresan


2 Answers

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.

like image 166
Andy Turner Avatar answered Nov 20 '22 01:11

Andy Turner


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

like image 4
Gal Naor Avatar answered Nov 20 '22 02:11

Gal Naor