diff --git a/snack3/src/main/java/org/noear/snack/to/ObjectToer.java b/snack3/src/main/java/org/noear/snack/to/ObjectToer.java index 2f65550fbfd46e670b862988d0a552c9c3277b84..c86db1a849f8e89fcc5a1835e4e042fa71e52468 100644 --- a/snack3/src/main/java/org/noear/snack/to/ObjectToer.java +++ b/snack3/src/main/java/org/noear/snack/to/ObjectToer.java @@ -277,41 +277,41 @@ public class ObjectToer implements Toer { if (null == date) { return null; } else { - return OffsetDateTime.ofInstant(date.toInstant(), DEFAULTS.DEF_TIME_ZONE.toZoneId()); + return OffsetDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), DEFAULTS.DEF_TIME_ZONE.toZoneId()); } } else if (is(ZonedDateTime.class, clz)) { Date date = v.getDate(); if (null == date) { return null; } else { - return ZonedDateTime.ofInstant(date.toInstant(), DEFAULTS.DEF_TIME_ZONE.toZoneId()); + return ZonedDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), DEFAULTS.DEF_TIME_ZONE.toZoneId()); } } else if (is(LocalDateTime.class, clz)) { Date date = v.getDate(); if (date == null) { return null; } else { - return date.toInstant().atZone(DEFAULTS.DEF_TIME_ZONE.toZoneId()).toLocalDateTime(); + return Instant.ofEpochMilli(date.getTime()).atZone(DEFAULTS.DEF_TIME_ZONE.toZoneId()).toLocalDateTime(); } } else if (is(LocalDate.class, clz)) { Date date = v.getDate(); if (date == null) { return null; } else { - return date.toInstant().atZone(DEFAULTS.DEF_TIME_ZONE.toZoneId()).toLocalDate(); + return Instant.ofEpochMilli(date.getTime()).atZone(DEFAULTS.DEF_TIME_ZONE.toZoneId()).toLocalDate(); } } else if (is(LocalTime.class, clz)) { Date date = v.getDate(); if (date == null) { return null; } else { - return date.toInstant().atZone(DEFAULTS.DEF_TIME_ZONE.toZoneId()).toLocalTime(); + return Instant.ofEpochMilli(date.getTime()).atZone(DEFAULTS.DEF_TIME_ZONE.toZoneId()).toLocalTime(); } } else if (is(OffsetTime.class, clz)) { // 可能无法通过v.getDate()获取时间,因为OffsetTime格式带有时区信息 Date date = v.getDate(); if (date != null) { - return date.toInstant().atOffset(DEFAULTS.DEF_OFFSET).toOffsetTime(); + return Instant.ofEpochMilli(date.getTime()).atOffset(DEFAULTS.DEF_OFFSET).toOffsetTime(); } // 获取不到则可能是"HH:mm:ss"或者"HH:mm:ssZ"等字符串格式 String dateStr = v.getString(); diff --git a/snack3_demo/src/test/java/features/DateTest.java b/snack3_demo/src/test/java/features/DateTest.java index 917b5c8d686bd59e87b0934f13ef106f0303eb46..d00d074ef84bbcd6eda459e21c06ec8c28b8368c 100644 --- a/snack3_demo/src/test/java/features/DateTest.java +++ b/snack3_demo/src/test/java/features/DateTest.java @@ -101,4 +101,11 @@ public class DateTest { DateUtil.parse(OffsetDateTime.now().toString()); DateUtil.parse(OffsetTime.now().toString()); } + + @Test + public void test7() throws Exception { + Date date = new java.sql.Date(DateUtil.parse(LocalDate.now().toString()).getTime()); + LocalDate day = ONode.load(date).toObject(LocalDate.class); + System.out.println(day.toString()); + } }