Emil Bode
Emil Bode

Reputation: 1830

java zoneddatetime: keep timezone ID when doing calculations

I have some small scripting-code in painless (a Groovy dialect, which itself is based on Java) that parses some timestamps with a zone-id. However, when doing calculations that involve crossing over a daylight-saving-time-boundary, the timezone-id is changed.

Reproducible example:

def form=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss (zzz)")
in=ZonedDateTime.parse(input, form)
out_plusweek=in.plusHours(168).format(form)

For most inputs I work with the result is fine: 2019-08-27 11:05:00 (Europe/Amsterdam) gives 2019-09-03 11:05:00 (Europe/Amsterdam), identical for similar dates.

However, if I input 2019-03-29 11:05:00 (Europe/Amsterdam), the out-variable is set to 2019-04-05 12:05:00 (CEST).

The value itself is fine, but the timezone-designation is changed to CEST (Central European Summer Time). It's correct, but no longer parseable by the next step in my script, which needs an id like Europe/Amsterdam

So how can I get 2019-04-05 12:05:00 (Europe/Amsterdam)?

Upvotes: 0

Views: 170

Answers (1)

Abra
Abra

Reputation: 20914

I suggest using a different format pattern for printing out the result. Try using VV instead of zzz.

According to the documentation lowercase z will give you the time zone name such as Pacific Standard Time or PST. For the time zone ID such as America/Los_Angeles you need uppercase V. And:

If the count of letters is two, then the time-zone ID is output. Any other count of letters throws IllegalArgumentException.

Upvotes: 1

Related Questions