]> git.basschouten.com Git - openhab-addons.git/commitdiff
Fix date cast exception (#13382)
authorJacob Laursen <jacob-github@vindvejr.dk>
Mon, 12 Sep 2022 16:16:25 +0000 (18:16 +0200)
committerGitHub <noreply@github.com>
Mon, 12 Sep 2022 16:16:25 +0000 (18:16 +0200)
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>
bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/db/JdbcBaseDAO.java

index 1efc052d8cb71e12619d34a65c045592ff227c1a..7dc19e6d4ca5234cd912bbc387eae0e0494a61e0 100644 (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) {