Out of curiosity are there any one who knows what the difference is between UTC as ZoneId and ZoneOffset in Java.
I was a little bit surprised that it was not true when comparing utc in zoneid and zoneoffset
I tried to write this test in spock and the only thing I could find was that the zone is different.
And does anyone have any recommendation on which one to use when?
It is not clear from the example but I actually used spock/groovy where == corresponds to the equals method in java, so apoligize for that
But no matter if I would have written lhs.equals(rhs) it would still return false
LocalDateTime now = LocalDateTime.now() // 2023-11-03T19:42:41.772234517
ZonedDateTime lhs = now.atZone(ZoneOffset.UTC)
String lhsString = lhs.toString() // 2023-11-03T19:42:41.772234517Z
ZonedDateTime rhs = now.atZone(ZoneId.of('UTC'))
String rhsString = rhs.toString() // 2023-11-03T19:42:41.772234517Z[UTC]
Duration duration = Duration.between(lhs, rhs) // PT0S
boolean equal = lhs == rhs // this is false
From the ZoneId documentation:
From the ZoneOffset documentation:
So the difference is summer vs winter time (daylight savings time), if the region uses them. There could be different states or countries in the same time zone, for example, Mexico City and Chicago are in the same zone offset (GMT-5), but Mexico stopped switching between summer and winter time in spring of 2023, so during summer they were an hour off, and in winter they'll match again.
So if you want to display or convert to local time, always use the ZoneId which considers the location (state) and the local rules on daylight savings time.