Open acmi opened 1 month ago
The root cause is a mismatch between data types. We adopt LocalDate
into java.sql.Timestamp
that is always associated with a time component, not only a year/month/date.
Have you tried implementing your own variant of LocalDateToTimestampConverter
to control how LocalDate
gets converted into Timestamp
?
It seems that converting java.time.LocalDate
to java.sql.Timestamp
is not entirely accurate and might even be impossible in some cases. I suppose this issue arises because there is no 00:00 local time on such dates.
For dates, there is a specific type: java.sql.Date
. As a solution, we registered a mapping from java.time.LocalDate
to java.sql.Date
in org.springframework.data.jdbc.core.convert.JdbcColumnTypes
class via reflection. I'm not sure if this solution is applicable for the entire Spring Data JDBC framework.
This is an extremely rare case for a specific time zone, and we discovered it completely by accident. While it might seem minor within a single time zone, it actually affects a quite extensive list of time zones and dates.
You can view the approximate list of affected dates with the following code snippet:
var lowBound = LocalDate.parse("1930-01-01");
for (var zoneId : TimeZone.getAvailableIDs()) {
ZoneId zone;
try {
zone = ZoneId.of(zoneId);
} catch (ZoneRulesException e) {
continue;
}
var affectedDates = new ArrayList<LocalDate>();
for (var date = LocalDate.now(); !date.isBefore(lowBound); date = date.minusDays(1)) {
if (date.atStartOfDay(zone).toLocalTime().isAfter(LocalTime.MIDNIGHT)) {
affectedDates.add(date);
}
}
if (!affectedDates.isEmpty()) {
System.out.println(zoneId + ": " + affectedDates);
}
}
Without fixing this case, using the java.time.LocalDate
type seems very unreliable.
When using the
java.time.LocalDate
type as a parameter for querying (which is converted tojava.sql.Timestamp
during the execution), some databases return an empty result in certain cases. For instance, this occurs with dates where a clock change happens at midnight.Code snippet to reproduce (Daylight Saving Time began in Egypt on April 26, 2024):