Fix date cast exception (#13382)

After upgrading mysql-connector to 8.0.30 this exception was thrown: class java.time.LocalDateTime cannot be cast to class java.sql.Timestamp

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
Jacob Laursen 2022-09-12 18:16:25 +02:00 committed by GitHub
parent 333ffbcba2
commit 474be93710
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 2 deletions

View File

@ -14,6 +14,7 @@ package org.openhab.persistence.jdbc.db;
import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
@ -564,11 +565,17 @@ public class JdbcBaseDAO {
}
protected ZonedDateTime objectAsDate(Object v) {
if (v instanceof java.lang.String) {
if (v instanceof LocalDateTime) {
return ZonedDateTime.of((LocalDateTime) v, ZoneId.systemDefault());
} else if (v instanceof java.sql.Timestamp) {
return ZonedDateTime.ofInstant(((java.sql.Timestamp) v).toInstant(), ZoneId.systemDefault());
} else if (v instanceof Instant) {
return ZonedDateTime.ofInstant((Instant) v, ZoneId.systemDefault());
} else if (v instanceof java.lang.String) {
return ZonedDateTime.ofInstant(java.sql.Timestamp.valueOf(v.toString()).toInstant(),
ZoneId.systemDefault());
}
return ZonedDateTime.ofInstant(((java.sql.Timestamp) v).toInstant(), ZoneId.systemDefault());
throw new UnsupportedOperationException("Date of type " + v.getClass().getName() + " is not supported");
}
protected Long objectAsLong(Object v) {