to summarize my question, I have 2 different ZonedDateTime objects with GMT and BST but same local time. They become the same Instant values after toInstant conversion. I am expecting after conversion, they should be different values because although they are both 'Tue Feb 23 18:00:46 2021', the strings are representing time for different time zones. I am not sure whether this is a bug of library itself or I am doing something wrong.
I have 2 different ZonedDateTime generated from this piece of Scala code:
import ...
import java.util.{Set => JavaSet}
val string2 = "Tue Feb 23 18:00:46 BST 2021"
val string1 = "Tue Feb 23 18:00:46 GMT 2021"
val timestampFormat: DateTimeFormatter = new DateTimeFormatterBuilder()
.appendOptional(DateTimeFormatter.ISO_DATE_TIME)
.appendOptional(DateTimeFormatter.ISO_INSTANT)
.appendOptional(DateTimeFormatter.ISO_OFFSET_DATE_TIME)
.appendOptional(DateTimeFormatter.ISO_ZONED_DATE_TIME)
.appendOptional(DateTimeFormatter.RFC_1123_DATE_TIME)
.appendPattern("EEE MMM ppd HH:mm:ss ")
.appendZoneText(TextStyle.SHORT, JavaSet.of(ZoneId.of("Europe/London")))
.appendPattern(" yyyy")
.toFormatter(Locale.US)
val parsedZdt2: ZonedDateTime = ZonedDateTime.parse(string2, timestampFormat)
val parsedZdt1: ZonedDateTime = ZonedDateTime.parse(string1, timestampFormat)
println(parsedZdt1 == parsedZdt2)
println("parsedZdt1.toString: " + parsedZdt1.toString)
println("parsedZdt2.toString: " + parsedZdt2.toString)
This prints out:
false
parsedZdt1.toString: 2021-02-23T18:00:46Z[GMT]
parsedZdt2.toString: 2021-02-23T18:00:46Z[Europe/London]
However when I convert them to instant:
println(parsedZdt1.toInstant == parsedZdt2.toInstant)
println("parsedZdt1.toInstant.toString: " + parsedZdt1.toInstant.toString)
println("parsedZdt2.toInstant.toString: " + parsedZdt2.toInstant.toString)
I got this printed out:
true
parsedZdt1.toInstant.toString: 2021-02-23T18:00:46Z
parsedZdt2.toInstant.toString: 2021-02-23T18:00:46Z
My questions are
- Why do the 2 values become the same after toInstant conversion?
- Are the 2 values representing same time even before toInstant, i.e. , are parsedZdt1 and parsedZdt2 representing the same moment?
Thank you!