Skip to content

Commit 4b51149

Browse files
committed
Fix #2643: add colon in timezone offset for better ISO-8601 compliance
1 parent e1d9be3 commit 4b51149

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

release-notes/VERSION-2.x

+6
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,14 @@ Project: jackson-databind
4242
(reported by Bartosz B)
4343
#2592: `ObjectMapper.setSerializationInclusion()` is ignored for `JsonAnyGetter`
4444
(reported by Oleksii K)
45+
#2643: Change default textual serialization of `java.util.Date`/`Calendar`
46+
to include colon in timezone offset
4547
- Add `SerializerProvider.findContentValueSerializer()` methods
4648

49+
2.10.4 (not yet released)
50+
51+
-
52+
4753
2.10.3 (03-Mar-2020)
4854

4955
#2482: `JSONMappingException` `Location` column number is one line Behind the actual

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

+11-8
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
* an ISO-8601 compliant format (format String "yyyy-MM-dd'T'HH:mm:ss.SSSZ")
1818
* and for deserialization, both ISO-8601 and RFC-1123.
1919
*<br>
20-
* Note that `Z` in format String refers to RFC-822 timezone notation which produces
21-
* values like "-0800" -- that is, full minute/hour combo without colon, and not using `Z`
22-
* as alias for "+0000".
23-
*<p>
24-
* Note also that to enable use of colon in timezone is possible by using method
25-
* {@link #withColonInTimeZone} for creating new differently configured format instance.
20+
* Note that `Z` in format String refers to ISO-8601 time offset notation which produces
21+
* values like "-08:00" -- that is, full minute/hour combo without colon, and not using `Z`
22+
* as alias for "+00:00".
23+
* Inclusion of colon as separator, as default setting, started in Jackson 2.11:
24+
* prior versions omitted it.
25+
* Note that it is possible to enable/disable use of colon in time offset by using method
26+
* {@link #withColonInTimeZone} for creating new differently configured format instance,
27+
* and configuring {@code ObjectMapper} with it.
2628
*/
2729
@SuppressWarnings("serial")
2830
public class StdDateFormat
@@ -153,11 +155,12 @@ public class StdDateFormat
153155
/**
154156
* Whether the TZ offset must be formatted with a colon between hours and minutes ({@code HH:mm} format)
155157
*<p>
156-
* Defaults to {@code false} for backwards compatibility reasons
158+
* Defaults to {@code true} since 2.11: earlier versions defaulted to {@code false}
159+
* for backwards compatibility reasons
157160
*
158161
* @since 2.9.1
159162
*/
160-
private boolean _tzSerializedWithColon = false;
163+
private boolean _tzSerializedWithColon = true;
161164

162165
/*
163166
/**********************************************************

src/test/java/com/fasterxml/jackson/databind/ser/jdk/DateSerializationTest.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -176,21 +176,22 @@ public void testDateISO8601_customTZ() throws IOException
176176
/**
177177
* Configure the StdDateFormat to serialize TZ offset with a colon between hours and minutes
178178
*
179-
* See [databind#1744]
179+
* See [databind#1744], [databind#2643]
180180
*/
181181
public void testDateISO8601_colonInTZ() throws IOException
182182
{
183+
// with [databind#2643], default now is to include
183184
StdDateFormat dateFormat = new StdDateFormat();
184-
assertFalse(dateFormat.isColonIncludedInTimeZone());
185-
dateFormat = dateFormat.withColonInTimeZone(true);
186185
assertTrue(dateFormat.isColonIncludedInTimeZone());
186+
// but we can disable it
187+
dateFormat = dateFormat.withColonInTimeZone(false);
187188

188189
ObjectMapper mapper = new ObjectMapper();
189190
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
190191
mapper.setDateFormat(dateFormat);
191192

192-
serialize( mapper, judate(1970, 1, 1, 02, 00, 00, 0, "GMT+2"), "1970-01-01T00:00:00.000+00:00");
193-
serialize( mapper, judate(1970, 1, 1, 00, 00, 00, 0, "UTC"), "1970-01-01T00:00:00.000+00:00");
193+
serialize(mapper, judate(1970, 1, 1, 02, 00, 00, 0, "GMT+2"), "1970-01-01T00:00:00.000+0000");
194+
serialize(mapper, judate(1970, 1, 1, 00, 00, 00, 0, "UTC"), "1970-01-01T00:00:00.000+0000");
194195
}
195196

196197
public void testDateOther() throws IOException
@@ -384,7 +385,7 @@ private void serialize(ObjectWriter w, Object date, String expected) throws IOEx
384385

385386
private String zoneOffset(String raw) {
386387
// Add colon or not -- difference between 2.10 and earlier, 2.11 and later
387-
// return raw.substring(0, 2) + ":" + raw.substring(2); // 2.11 and later
388-
return raw; // 2.10 and earlier
388+
return raw.substring(0, 2) + ":" + raw.substring(2); // 2.11 and later
389+
// return raw; // 2.10 and earlier
389390
}
390391
}

0 commit comments

Comments
 (0)