LocalDateTime issue with timestamp and timezones


#1

I have a problem with a timestamp field which is mapped to a LocalDateTime. We don’t use UTC times in the database. Therefor, I expect the timestamp in the database to be equal to the value in the LocalDateTime.

The problem is that the conversion from a Timestamp to a LocalDateTime in LocalDateTimeJavaDescriptor uses the default timezone. For some reasons, this makes the two timestamps differ for timestamps before January 2, 1900.

I want to use no conversion (like the method Timestamp.toLocalDateTime).

One option I tried is to implement an attribute converter for Timestamp to LocalDateTime. This causes an error, because in our model LocalDateTime fields are used for versioning.

I don’t want to have to specify a custom converter to every LocalDateTime field in our domain model.

Does anyone have a solution for this problem?

Thanks in advance.


#2

The problem is that the conversion from a Timestamp to a LocalDateTime in LocalDateTimeJavaDescriptor uses the default timezone.

The JDBC Driver does not support LocalDateTime natively, so this needs to be saved as PreparedStatement#setTimestamp.

Now, instead of:

Instant instant = value.atZone( ZoneId.systemDefault() ).toInstant();
return (X) java.sql.Timestamp.from( instant );

we could try to use:

return (X) java.sql.Timestamp.valueOf( value );

However, we need a test case to replicate your issue and see how we could fix it without breaking existing systems.