Although java.sql.Timestamp stores millions of eras, the .toString method uses the default timezone to render the string. In addition, .valueOf interprets LocalDateTime using the default time zone.
The combination of both things leads to the fact that the first transformation "looks right", but in fact it is incorrect. The value "2017-04-04 19: 46: 33.567" is displayed in your TZ by default, and not in UTC.
Since you passed the valueOf a LocalDateTime (UTC) method, but it interpreted it as LocalDateTime (your default TZ).
Here is the proof that the first conversion is wrong:
scala> val now = OffsetDateTime.now now: java.time.OffsetDateTime = 2017-04-04T14:50:12.534-06:00 scala> Timestamp.valueOf(now.atZoneSameInstant(ZoneId.of("UTC")).toLocalDateTime).getTime == now.toInstant.toEpochMilli res54: Boolean = false
Now with the .atZoneSameInstant removed:
scala> Timestamp.valueOf(now.toLocalDateTime).getTime == now.toInstant.toEpochMilli res53: Boolean = true
Invalid accepted answer to the specified stacking question.
Once you fix the first conversion (remove .atZoneSameInstant ), your second conversion should work fine.
source share