Different timezone id for the same short timezone name

I have a string "02:00:00 Feb 05, 2023 PST" which I parse:

ZonedDateTime d = ZonedDateTime.parse(nextPaymentDate, DateTimeFormatter.ofPattern("H:m:s MMM d, uuuu z"));

Comparing it a constructed date:

ZonedDateTime expectedDate = ZonedDateTime.of(2023, 2, 5, 2, 0, 0, 0, ZoneId.of(ZoneId.SHORT_IDS.get("PST")));

fails, because:

Expected :2023-02-05T02:00-08:00[America/Los_Angeles]
Actual   :2023-02-05T02:00-08:00[America/Tijuana]

Setting exact timezone id (e.g. fixing America/Tijuana) does not work, as the timezone is parsed differently on another machine (as America/Los_Angeles instead of America/Tijuana like on my machine).

How can I assert the dates are equal in this case?

>Solution :

The time zone abbreviations used by Java when parsing depend on the locale, so you cannot compare the result to a fixed value. See this answer.

I would convert the ZonedDateTime objects to OffsetDateTime before comparing them, or even to Instant, as Joachim suggested.

Leave a Reply