If you read the documentation, the javadoc of Timestamp.valueOf(LocalDateTime dateTime)
says:
The provided LocalDateTime
is interpreted as the local date-time in the local time zone.
Since the LocalDateTime
in the UTC time zone, not the local time zone, the result is a time zone shift to JVM's default time zone. If you remove ZoneOffset.UTC
from the now()
call, or use ZoneId.systemDefault()
instead, it'll work as you expected.
Alternatively, if you do have a LocalDateTime
in UTC, and want to convert to Timestamp
, you need to say the LocalDateTime
is in UTC:
LocalDateTime ldt = LocalDateTime.now(UTC); // cannot change time zone
long millis = Timestamp.from(ldt.atZone(UTC).toInstant()).getTime(); // so specify time zone
Of course, the values will still not necessarily be equal, since a few milliseconds of time may have passed between the two calls.