From 8572567246e267638eb404a7e52759e5566d773f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E9=93=81?= Date: Sun, 1 Sep 2024 16:38:22 +0800 Subject: [PATCH] deserialize java.util.Date support input time string, for issue #2905 --- .../reader/ObjectReaderImplDate.java | 7 +++++- .../fastjson2/issues_2900/Issue2905.java | 23 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 core/src/test/java/com/alibaba/fastjson2/issues_2900/Issue2905.java diff --git a/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderImplDate.java b/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderImplDate.java index 987bcb77b0..3c5f150725 100644 --- a/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderImplDate.java +++ b/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderImplDate.java @@ -144,7 +144,12 @@ private Object readDate(JSONReader jsonReader) { int length = yyyyMMddhhmm16 ? 16 : 19; ldt = DateUtils.parseLocalDateTime(str, 0, length); } else { - ldt = LocalDateTime.parse(str, formatter); + if (formatHasDay) { + ldt = LocalDateTime.parse(str, formatter); + } else { + LocalTime localTime = LocalTime.parse(str, formatter); + ldt = LocalDateTime.of(LocalDate.MIN, localTime); + } } } zdt = ldt.atZone(jsonReader.getContext().getZoneId()); diff --git a/core/src/test/java/com/alibaba/fastjson2/issues_2900/Issue2905.java b/core/src/test/java/com/alibaba/fastjson2/issues_2900/Issue2905.java new file mode 100644 index 0000000000..23172599b6 --- /dev/null +++ b/core/src/test/java/com/alibaba/fastjson2/issues_2900/Issue2905.java @@ -0,0 +1,23 @@ +package com.alibaba.fastjson2.issues_2900; + +import com.alibaba.fastjson.annotation.JSONField; +import com.alibaba.fastjson2.JSON; +import org.junit.jupiter.api.Test; + +import java.util.Date; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class Issue2905 { + @Test + public void test() { + String s = "{\"saleEndTime\": \"23:00:00\"}"; + Bean order = JSON.parseObject(s, Bean.class); + assertNotNull(order.saleEndTime); + } + + public static class Bean { + @JSONField(format = "HH:mm:ss") + public Date saleEndTime; + } +}