From 474be937107c984871a92dcd1c1ff70b09ecd92f Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Mon, 12 Sep 2022 18:16:25 +0200 Subject: [PATCH] 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 --- .../org/openhab/persistence/jdbc/db/JdbcBaseDAO.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/db/JdbcBaseDAO.java b/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/db/JdbcBaseDAO.java index 1efc052d8..7dc19e6d4 100644 --- a/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/db/JdbcBaseDAO.java +++ b/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/db/JdbcBaseDAO.java @@ -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) {