Skip to content

Commit 6671559

Browse files
committed
Merge pull request #817 from wingspan/2.5
Allow date-only ISO strings to have no time zone. #816
2 parents d4923ea + 3cbb7bc commit 6671559

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/main/java/com/fasterxml/jackson/databind/util/ISO8601Utils.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,15 @@ public static Date parse(String date, ParsePosition pos) throws ParseException {
152152
int minutes = 0;
153153
int seconds = 0;
154154
int milliseconds = 0; // always use 0 otherwise returned date will include millis of current time
155+
156+
// if the value has no time component (and no time zone), we are done
157+
if (!checkOffset(date, offset, 'T') && (date.length() <= offset)) {
158+
Calendar calendar = new GregorianCalendar(year, month - 1, day);
159+
160+
pos.setIndex(offset);
161+
return calendar.getTime();
162+
}
163+
155164
if (checkOffset(date, offset, 'T')) {
156165

157166
// extract hours, minutes, seconds and milliseconds

src/test/java/com/fasterxml/jackson/databind/util/ISO8601DateFormatTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66

77
import com.fasterxml.jackson.databind.BaseMapTest;
8-
import com.fasterxml.jackson.databind.util.ISO8601DateFormat;
98

109
/**
1110
* @see ISO8601DateFormat
@@ -33,6 +32,16 @@ public void testFormat() {
3332
public void testParse() throws Exception {
3433
Date result = df.parse("2007-08-13T19:51:23Z");
3534
assertEquals(date, result);
35+
36+
// Test parsing date-only values with and without a timezone designation
37+
Date dateOnly = df.parse("2007-08-14");
38+
Calendar cal = new GregorianCalendar(2007, 8-1, 14);
39+
assertEquals(cal.getTime(), dateOnly);
40+
41+
dateOnly = df.parse("2007-08-14Z");
42+
cal = new GregorianCalendar(2007, 8-1, 14);
43+
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
44+
assertEquals(cal.getTime(), dateOnly);
3645
}
3746

3847
public void testPartialParse() throws Exception {

0 commit comments

Comments
 (0)