How should I parse this datetime value that is in the PDT timezone?
06/24/2017 07:00 AM (PDT)
I want to maintain the timezone so that I can then represent the time in other timezones depending on the website visitors preferences.
I tried using ZonedDateTime
but I get a parse error:
java.time.ZonedDateTime.parse("06/24/2017 07:00 AM (PDT)")
The error is:
java.time.format.DateTimeParseException: Text '06/24/2017 07:00 AM (PDT)' could not be parsed at index 0
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.ZonedDateTime.parse(ZonedDateTime.java:597)
at java.time.ZonedDateTime.parse(ZonedDateTime.java:582) ... 29 elided
Also, do you agree that I should be using a ZonedDateTime
?
The
parse
method expects aString
in a specific format, like2007-12-03T10:15:30+01:00[Europe/Paris]
. As your input is in a different format, you need aDateTimeFormatter
.One detail to notice is that the API uses IANA timezones names (always in the format
Continent/City
, likeAmerica/Sao_Paulo
orEurope/Berlin
). Avoid using the 3-letter abbreviations (likeCST
orPST
) because they are ambiguous and not standard.The API makes some exceptions with specific IDs and provides some defaults for them. For
PDT
, it defaults toAmerica/Los_Angeles
.Another detail is that in the example below I used lowercase
hh
in the pattern: the format has AM/PM indication, so I think thathh
is the correct pattern, as its value is from 1 to 12 (the common values when there's the AM/PM indicator).If you use uppercase
HH
, it allows values from 0 to 23 (and it's not common to use this with AM/PM), and it will throw an exception if the input contains an hour like07:00 PM
.So the code will be like:
The output is:
But not all the 3-letter timezone names will be recognized by the API and will throw an exception.
Anyway, there are other timezones that also are in PDT (like
America/Vancouver
) - you can get a list of all by callingZoneId.getAvailableZoneIds()
. If you want to use a different timezone as the default, you can create a set of preferred zones and build a formatter with this set:The API will use the preferred zones set (in this case,
America/Vancouver
) instead of the default (America/Los_Angeles
). The output will be:It's not clear where the input
String
's come from. If you can't control their format, then you have no choice: they need to be parsed this way. Then you can convert it to another timezone using thewithZoneSameInstant
method:The value of
other
will be2017-06-24T11:00-03:00[America/Sao_Paulo]
.But if you can control the output, it's always better (IMO) to internally work with UTC (
java.time.Instant
), and convert to some timezone only when displaying to users:The error you get is well covered in the other answers already.
Yes and no. Your string should definitely be parsed into a
ZonedDateTime
. I recommend you convert it to anInstant
and store this. Then when you need to present it to a user according to his/her time zone preference, you may either convert theInstant
to aZonedDateTime
again or just format it using aDateTimeFormatter
with the desired default time zone.Why do it this way? First, common practice is to store
Instant
s. Some prefer to store just milliseconds since the epoch, I think this some (often misunderstood) performance measure. Certainly such milliseconds I quite unreadable whileInstant
s can be deciphered on eye-sight, at least roughly. The only other alternative I respect is when you know for certain that your application will never need to be concerned with a time zone (does this ever happen?), then sometimesLocalDateTime
is used for storage.If I understand your situation correctly, you need to store the point in time for display into multiple time zones. You don’t need to store the time zone in which the time was originally entered (like PDT, except PDT is not really a full time zone).
Instant
is time zone neutral, which is one reason I prefer it over storing the time in some time zone, asZonedDateTime
would. Also anInstant
is simpler conceptually, and my guess is that it is also simpler implementation-wise.There are a couple of much better answers here: Best practices with saving datetime & timezone info in database when data is dependant on datetime.
Since your format is non-standard, you need to specify it to the parser: