From f3d55e856a58c8c71e15a812f9da44838bd22c61 Mon Sep 17 00:00:00 2001 From: Ruoyang Date: Fri, 25 Aug 2023 17:54:02 -0700 Subject: [PATCH] As requested in open issue 1511, have parser default to UTC if timezone is not provided --- .../com/google/gson/internal/bind/util/ISO8601Utils.java | 1 + .../google/gson/internal/bind/util/ISO8601UtilsTest.java | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/gson/src/main/java/com/google/gson/internal/bind/util/ISO8601Utils.java b/gson/src/main/java/com/google/gson/internal/bind/util/ISO8601Utils.java index 3fd22d7407..420123c414 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/util/ISO8601Utils.java +++ b/gson/src/main/java/com/google/gson/internal/bind/util/ISO8601Utils.java @@ -170,6 +170,7 @@ public static Date parse(String date, ParsePosition pos) throws ParseException { if (!hasT && (date.length() <= offset)) { Calendar calendar = new GregorianCalendar(year, month - 1, day); + calendar.setTimeZone(TIMEZONE_UTC); calendar.setLenient(false); pos.setIndex(offset); diff --git a/gson/src/test/java/com/google/gson/internal/bind/util/ISO8601UtilsTest.java b/gson/src/test/java/com/google/gson/internal/bind/util/ISO8601UtilsTest.java index 4a16ee9f8a..d7ee700f76 100644 --- a/gson/src/test/java/com/google/gson/internal/bind/util/ISO8601UtilsTest.java +++ b/gson/src/test/java/com/google/gson/internal/bind/util/ISO8601UtilsTest.java @@ -78,10 +78,12 @@ public void testDateFormatWithTimezone() { @Test @SuppressWarnings("UndefinedEquals") - public void testDateParseWithDefaultTimezone() throws ParseException { + public void testDateParseDefaultsToUTC() throws ParseException { String dateStr = "2018-06-25"; Date date = ISO8601Utils.parse(dateStr, new ParsePosition(0)); - Date expectedDate = new GregorianCalendar(2018, Calendar.JUNE, 25).getTime(); + GregorianCalendar calendar = createUtcCalendar(); + calendar.set(2018, Calendar.JUNE, 25); + Date expectedDate = calendar.getTime(); assertThat(date).isEqualTo(expectedDate); }