0

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]?

CameronCoob
  • 81
  • 1
  • 11

1 Answers1

2

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 from UTC-8 timezone, so it's the same as 2020-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.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • But 2020-01-01T00:00:00+01:00[UTC-08:00] works as well on Java 11, so I believe that as I said offset is being applied relative to zoneId, but now the question is why zone id is not being considered in UTC – CameronCoob Jul 15 '20 at 21:44
  • @CameronCoob What do you mean “works”? Post some code in your Question. As this Answer correctly explains, `2020-01-01'T'00:00:00+01:00[UTC-8]` is contradictory nonsense. – Basil Bourque Jul 16 '20 at 01:06
  • @CameronCoob Please see my edit correcting your belief. – Ole V.V. Jul 16 '20 at 02:34