From 3cb38e5e91d5f3f6d51b3f209d8ad87131ea96e3 Mon Sep 17 00:00:00 2001 From: zhouhao Date: Tue, 24 Sep 2024 16:58:20 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E8=A7=A3=E6=9E=90java.sql.Date=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ezorm/rdb/codec/DateTimeCodec.java | 19 ++++++++++++------- .../ezorm/rdb/codec/DateTimeCodecTest.java | 15 +++++++++++++++ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/codec/DateTimeCodec.java b/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/codec/DateTimeCodec.java index 56cbec04..1557bd11 100644 --- a/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/codec/DateTimeCodec.java +++ b/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/codec/DateTimeCodec.java @@ -23,9 +23,9 @@ public class DateTimeCodec implements ValueCodec { private String format; - private Class toType; + private Class toType; - public DateTimeCodec(String format, Class toType) { + public DateTimeCodec(String format, Class toType) { this.format = format; this.toType = toType; } @@ -48,10 +48,11 @@ public Object encode(Object value) { } if (value instanceof String) { if (((String) value).contains(",")) { - return Arrays.stream(((String) value).split(",")) - .map(this::doParse) - .filter(Objects::nonNull) - .collect(Collectors.toList()); + return Arrays + .stream(((String) value).split(",")) + .map(this::doParse) + .filter(Objects::nonNull) + .collect(Collectors.toList()); } return doParse(((String) value)); @@ -68,6 +69,10 @@ public Object decode(Object data) { if (toType.isAssignableFrom(data.getClass())) { return data; } + // java.sql.Date 无法使用toInstant + if (data instanceof java.sql.Date) { + data = new Date(((java.sql.Date) data).getTime()); + } if (!(data instanceof Date)) { data = toDate(data); } @@ -118,7 +123,7 @@ public Object toDate(Object data) { } else if (data instanceof ZonedDateTime) { ZonedDateTime dateTime = ((ZonedDateTime) data); data = Date.from(dateTime.toInstant()); - }else if(data instanceof OffsetDateTime){ + } else if (data instanceof OffsetDateTime) { data = Date.from(((OffsetDateTime) data).toInstant()); } else if (data instanceof String) { String stringData = ((String) data); diff --git a/hsweb-easy-orm-rdb/src/test/java/org/hswebframework/ezorm/rdb/codec/DateTimeCodecTest.java b/hsweb-easy-orm-rdb/src/test/java/org/hswebframework/ezorm/rdb/codec/DateTimeCodecTest.java index 27cd14d7..e2fbd242 100644 --- a/hsweb-easy-orm-rdb/src/test/java/org/hswebframework/ezorm/rdb/codec/DateTimeCodecTest.java +++ b/hsweb-easy-orm-rdb/src/test/java/org/hswebframework/ezorm/rdb/codec/DateTimeCodecTest.java @@ -115,4 +115,19 @@ public void testDecodeStringTime() { assertEquals(Lists.newArrayList(now, now), decode); } } + + @Test + public void testDecodeSqlDate() { + DateTimeCodec codec = new DateTimeCodec("yyyy-MM-dd", LocalDateTime.class); + + java.sql.Date data = new java.sql.Date(System.currentTimeMillis()); + + Object val = codec.encode(data); + assertEquals(data, val); + + Object date = codec.encode("2019-01-01"); + + assertTrue(codec.decode(date) instanceof LocalDateTime); + } + } \ No newline at end of file