Reputation: 81
I'm a little bit confused of possibility in Java to have ZoneId and ZoneOffset in date at the same time
For example, does the date 2020-01-01'T'00:00:00+01:00[UTC-8]
make sense? What time is it in UTC?
Is it +01:00
offset from UTC-8
timezone, so it's the same as 2020-01-01'T'00:00:00[UTC-7]
?
Upvotes: 0
Views: 167
Reputation: 86306
No 2020-01-01'T'00:00:00+01:00[UTC-8]
is a self contradiction. I doubt that there’s a way to construct a ZonedDateTime
having this value. Or only by defining your own time zone that has an ID of UTC-8
and an offset of +01:00 at that point in time.
A ZonedDateTime
has got both time zone and offset.
I am unsure whether it’s relevant to your question, but one way to study whether we can make a point in time out of a self-contradictory string could be:
System.out.println(ZonedDateTime.parse("2020-01-01T00:00:00+01:00[Pacific/Pitcairn]"));
Output on Java 11:
2019-12-31T15:00-08:00[Pacific/Pitcairn]
Pitcairn is at offset -08:00 all year. It seems that ZonedDateTime
has chosen to trust the offset +01:00
, so the time is the same as 2019-12-31T23:00 in UTC. It has then converted this time to the offset that it finds right for Pacific/Pitcairn time zone according to the time zone database, giving 15:00 in that time zone.
Edit:
What time is it in UTC? Is it
+01:00
offset fromUTC-8
timezone, so it's the same as2020-01-01'T'00:00:00[UTC-7]
?
No, it is not the same at all. 2020-01-01'T'00:00:00[UTC-7]
would be the same point in time as 2020-01-01T07:00:00 in UTC, but as I said, we got 2019-12-31T23:00 in UTC, which is 8 hours earlier than what you suggested. The -08:00
are not used to interpret the point in time, they are only used for conversion.
PS There aren’t any single quotes in 2020-01-01T00:00:00+01:00[UTC-8]
. They are only in a format pattern that you would use for printing and/or parsing such a string.
Upvotes: 2