From bf02672e5840f5e54c86d48191c238e42b9015f5 Mon Sep 17 00:00:00 2001 From: Caleb Date: Tue, 23 Apr 2024 14:09:30 -0500 Subject: [PATCH 1/2] Update DefaultDateTypeAdapter.java to include support for "timeless" Date when deserializing. --- .../com/google/gson/internal/bind/DefaultDateTypeAdapter.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/gson/src/main/java/com/google/gson/internal/bind/DefaultDateTypeAdapter.java b/gson/src/main/java/com/google/gson/internal/bind/DefaultDateTypeAdapter.java index b5dffe24fb..b8f5dd4c49 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/DefaultDateTypeAdapter.java +++ b/gson/src/main/java/com/google/gson/internal/bind/DefaultDateTypeAdapter.java @@ -134,6 +134,8 @@ private DefaultDateTypeAdapter(DateType dateType, int dateStyle, int timeStyl if (JavaVersion.isJava9OrLater()) { dateFormats.add(PreJava9DateFormatProvider.getUsDateTimeFormat(dateStyle, timeStyle)); } + // Include fall-back for "timeless" Date (could be from a java.sql.Date, for example) + dateFormats.add(DateFormat.getDateInstance(dateStyle, Locale.US)); } @Override From 11e992807c1c21537e76d3a596b4ee135e42dea3 Mon Sep 17 00:00:00 2001 From: Caleb Date: Tue, 23 Apr 2024 14:10:58 -0500 Subject: [PATCH 2/2] Add additional test case to DefaultDateTypeAdapterTest.java for "timeless" Date. --- .../google/gson/internal/bind/DefaultDateTypeAdapterTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/gson/src/test/java/com/google/gson/internal/bind/DefaultDateTypeAdapterTest.java b/gson/src/test/java/com/google/gson/internal/bind/DefaultDateTypeAdapterTest.java index 81e1c28a8b..510ec3f9d6 100644 --- a/gson/src/test/java/com/google/gson/internal/bind/DefaultDateTypeAdapterTest.java +++ b/gson/src/test/java/com/google/gson/internal/bind/DefaultDateTypeAdapterTest.java @@ -119,6 +119,7 @@ public void testParsingDatesFormattedWithUsLocale() throws Exception { Locale.setDefault(Locale.US); try { assertParsed("Jan 1, 1970 0:00:00 AM", DefaultDateTypeAdapter.DEFAULT_STYLE_FACTORY); + assertParsed("Jan 1, 1970", DefaultDateTypeAdapter.DEFAULT_STYLE_FACTORY); assertParsed( "1/1/70 0:00 AM", DateType.DATE.createAdapterFactory(DateFormat.SHORT, DateFormat.SHORT)); assertParsed(