Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java TimeZone.getTimeZone("PDT") not working

Tags:

java

timezone

It returns the default timezone for GMT.

Using the SimpleTimeFormat with "z" for timezone, it prints "PDT". But "PDT" is not in the list returned from TimeZone.getAvailableIDs(). Really strange!

Anyone knows why "PDT" is not a standard tz? How to deal with this? Someone invokes my API passing in "PDT" as the timezone. Thanks.

like image 984
Nathan W Avatar asked Jul 01 '15 02:07

Nathan W


People also ask

What is PDT in Java?

The “PDT” is a pseudo-time zone used by the media to indicate vaguely a set of time zones plus an indicator if they intended during the period when Daylight Saving Time (DST) is engaged or not ( PST ).

How do I change the TimeZone in Java?

You can make use of the following DateFormat. SimpleDateFormat myDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); myDate. setTimeZone(TimeZone. getTimeZone("UTC")); Date newDate = myDate.

How do you show TimeZone in formatted date in Java?

Use "zzz" instead of "ZZZ": "Z" is the symbol for an RFC822 time zone. DateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy"); Having said that, my standard advice on date/time stuff is to use Joda Time, which is an altogether better API.

How does Java handle time zone issues?

If you cannot change the OS or the JVM timezone, you can still convert a Java Date/Time or Timestamp to a specific time zone using the following two JDBC methods: PreparedStatement#setTimestamp(int parameterIndex, Timestamp x, Calendar cal) – to convert the timestamp that goes to the database.

Why is “PDT” not a time zone?

Because “PDT” is not a time zone! The “PDT” is a pseudo-time zone used by the media to indicate vaguely a set of time zones plus an indicator if they intended during the period when Daylight Saving Time (DST) is engaged or not ( PST ). Avoid these 2-4 letter codes as they are not true time zones, not standardized, and are not even unique (!).

How to get the timezone of a Timezone in Java?

The getTimeZone () method of TimeZone class in Java is used to know the actual TimeZone for any passed TimeZone ID. Parameters: The method takes one parameter the_ID of string datatype which refers to the ID of which the TimeZone is needed to be known.

What do the “PDT” codes mean?

The “PDT” is a pseudo-time zone used by the media to indicate vaguely a set of time zones plus an indicator if they intended during the period when Daylight Saving Time (DST) is engaged or not ( PST ). Avoid these 2-4 letter codes as they are not true time zones, not standardized, and are not even unique (!).

What is the difference between zoneid Z and PDT?

ZoneId z = ZoneId.of ( "America/Los_Angeles" ) ; ZoneId z = ZoneId.of ( "America/Tijuana" ) ; ZoneId z = ZoneId.of ( "America/Whitehorse" ) ; But PDT might not mean this zone as Arizona does not participate in the Daylight Saving Time (DST) nonsense, and the D in the middle means DST.


2 Answers

PDT is not a time zone

Anyone knows why "PDT" is not a standard tz?

Because “PDT” is not a time zone!

The “PDT” is a pseudo-time zone used by the media to indicate vaguely a set of time zones plus an indicator if they intended during the period when Daylight Saving Time (DST) is engaged or not (PST). Avoid these 2-4 letter codes as they are not true time zones, not standardized, and are not even unique(!).

Proper time zone names

Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland.

By PDT any of these time zones, and more, may be intended:

ZoneId z = ZoneId.of( "America/Los_Angeles" ) ;
ZoneId z = ZoneId.of( "America/Tijuana" ) ;
ZoneId z = ZoneId.of( "America/Whitehorse" ) ;

But PDT might not mean this zone as Arizona does not participate in the Daylight Saving Time (DST) nonsense, and the D in the middle means DST.

ZoneId z = ZoneId.of( "America/Phoenix" ) ;

Avoid legacy date-time classes

Avoid SimpleTimeFormat class as it is a part of the troublesome old date-time classes that are now legacy, supplanted by the java.time classes. Use DateTimeFormatter instead.

Avoid TimeZone as well. Replaced by ZoneId.

Pass ZoneId, not string

Someone invokes my API passing in "PDT" as the timezone.

Change your API to take a ZoneId as an argument, rather than a mere String. That ensures valid values and gives you type-safety.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes. Hibernate 5 & JPA 2.2 support java.time.

Where to obtain the java.time classes?

  • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
    • Java 9 brought some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • Later versions of Android (26+) bundle implementations of the java.time classes.
    • For earlier Android (<26), the process of API desugaring brings a subset of the java.time functionality not originally built into Android.
      • If the desugaring does not offer what you need, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above) to Android. See How to use ThreeTenABP….
like image 151
Basil Bourque Avatar answered Oct 11 '22 01:10

Basil Bourque


"PDT" is an abbreviation for Pacific Daylight Time. It is used in conjunction with Pacific Standard Time to make up the Pacific time zone.

The time zone identifier for the Pacific time zone is "America/Los_Angeles". You should use that instead.

Read the timezone tag wiki, and Wikipedia's article on the tz database.

like image 22
Matt Johnson-Pint Avatar answered Oct 11 '22 02:10

Matt Johnson-Pint