1
1
package com .fasterxml .jackson .databind .ser .jdk ;
2
2
3
- import java .io .*;
4
- import java .text .*;
5
- import java .util .*;
3
+ import java .io .IOException ;
4
+ import java .text .DateFormat ;
5
+ import java .text .SimpleDateFormat ;
6
+ import java .util .Calendar ;
7
+ import java .util .Date ;
8
+ import java .util .GregorianCalendar ;
9
+ import java .util .HashMap ;
10
+ import java .util .Locale ;
11
+ import java .util .Map ;
12
+ import java .util .TimeZone ;
13
+
14
+ import org .junit .Assert ;
6
15
7
16
import com .fasterxml .jackson .annotation .JsonFormat ;
8
- import com .fasterxml .jackson .databind .*;
17
+ import com .fasterxml .jackson .databind .BaseMapTest ;
18
+ import com .fasterxml .jackson .databind .ObjectMapper ;
19
+ import com .fasterxml .jackson .databind .ObjectWriter ;
20
+ import com .fasterxml .jackson .databind .SerializationFeature ;
21
+ import com .fasterxml .jackson .databind .util .StdDateFormat ;
9
22
10
23
public class DateSerializationTest
11
24
extends BaseMapTest
@@ -103,9 +116,42 @@ public void testDateISO8601() throws IOException
103
116
{
104
117
ObjectMapper mapper = new ObjectMapper ();
105
118
mapper .configure (SerializationFeature .WRITE_DATES_AS_TIMESTAMPS , false );
106
- // let's hit epoch start
107
- String json = mapper .writeValueAsString (new Date (0L ));
108
- assertEquals ("\" 1970-01-01T00:00:00.000+0000\" " , json );
119
+
120
+ serialize ( mapper , judate (1970 , 1 , 1 , 02 , 00 , 00 , 0 , "GMT+2" ), "1970-01-01T00:00:00.000+0000" );
121
+ serialize ( mapper , judate (1970 , 1 , 1 , 00 , 00 , 00 , 0 , "UTC" ), "1970-01-01T00:00:00.000+0000" );
122
+ }
123
+
124
+ /**
125
+ * Use a default TZ other than UTC. Dates must be serialized using that TZ.
126
+ */
127
+ public void testDateISO8601_customTZ () throws IOException
128
+ {
129
+ ObjectMapper mapper = new ObjectMapper ();
130
+ mapper .configure (SerializationFeature .WRITE_DATES_AS_TIMESTAMPS , false );
131
+ mapper .setTimeZone (TimeZone .getTimeZone ("GMT+2" ));
132
+
133
+ serialize ( mapper , judate (1970 , 1 , 1 , 00 , 00 , 00 , 0 , "GMT+2" ), "1970-01-01T00:00:00.000+0200" );
134
+ serialize ( mapper , judate (1970 , 1 , 1 , 00 , 00 , 00 , 0 , "UTC" ), "1970-01-01T02:00:00.000+0200" );
135
+ }
136
+
137
+ /**
138
+ * Configure the StdDateFormat to serialize TZ offset with a colon between hours and minutes
139
+ *
140
+ * See [databind#1744]
141
+ */
142
+ public void testDateISO8601_colonInTZ () throws IOException
143
+ {
144
+ StdDateFormat dateFormat = new StdDateFormat ();
145
+ assertFalse (dateFormat .isColonIncludedInTimeZone ());
146
+ dateFormat = dateFormat .withColonInTimeZone (true );
147
+ assertTrue (dateFormat .isColonIncludedInTimeZone ());
148
+
149
+ ObjectMapper mapper = new ObjectMapper ();
150
+ mapper .configure (SerializationFeature .WRITE_DATES_AS_TIMESTAMPS , false );
151
+ mapper .setDateFormat (dateFormat );
152
+
153
+ serialize ( mapper , judate (1970 , 1 , 1 , 02 , 00 , 00 , 0 , "GMT+2" ), "1970-01-01T00:00:00.000+00:00" );
154
+ serialize ( mapper , judate (1970 , 1 , 1 , 00 , 00 , 00 , 0 , "UTC" ), "1970-01-01T00:00:00.000+00:00" );
109
155
}
110
156
111
157
public void testDateOther () throws IOException
@@ -114,8 +160,9 @@ public void testDateOther() throws IOException
114
160
DateFormat df = new SimpleDateFormat ("yyyy-MM-dd'X'HH:mm:ss" );
115
161
mapper .setDateFormat (df );
116
162
mapper .setTimeZone (TimeZone .getTimeZone ("PST" ));
163
+
117
164
// let's hit epoch start, offset by a bit
118
- assertEquals ( quote ( " 1969-12-31X16:00:00"), mapper . writeValueAsString ( new Date ( 0L )) );
165
+ serialize ( mapper , judate ( 1970 , 1 , 1 , 00 , 00 , 00 , 0 , "UTC" ), " 1969-12-31X16:00:00" );
119
166
}
120
167
121
168
public void testTimeZone () throws IOException
@@ -200,19 +247,18 @@ public void testWithTimeZoneOverride() throws Exception
200
247
mapper .setDateFormat (new SimpleDateFormat ("yyyy-MM-dd/HH:mm z" ));
201
248
mapper .setTimeZone (TimeZone .getTimeZone ("PST" ));
202
249
mapper .disable (SerializationFeature .WRITE_DATES_AS_TIMESTAMPS );
203
- String json = mapper . writeValueAsString ( new Date ( 0 ));
250
+
204
251
// pacific time is GMT-8; so midnight becomes 16:00 previous day:
205
- assertEquals ( quote ( " 1969-12-31/16:00 PST"), json );
252
+ serialize ( mapper , judate ( 1969 , 12 , 31 , 16 , 00 , 00 , 00 , "PST" ), " 1969-12-31/16:00 PST" );
206
253
207
254
// Let's also verify that Locale won't matter too much...
208
255
mapper .setLocale (Locale .FRANCE );
209
- json = mapper .writeValueAsString (new Date (0 ));
210
- assertEquals (quote ("1969-12-31/16:00 PST" ), json );
256
+ serialize ( mapper , judate (1969 , 12 , 31 , 16 , 00 , 00 , 00 , "PST" ), "1969-12-31/16:00 PST" );
211
257
212
258
// Also: should be able to dynamically change timezone:
213
259
ObjectWriter w = mapper .writer ();
214
260
w = w .with (TimeZone .getTimeZone ("EST" ));
215
- json = w .writeValueAsString (new Date (0 ));
261
+ String json = w .writeValueAsString (new Date (0 ));
216
262
assertEquals (quote ("1969-12-31/19:00 EST" ), json );
217
263
}
218
264
@@ -272,4 +318,20 @@ public void testFormatWithoutPattern() throws Exception
272
318
String json = mapper .writeValueAsString (new DateAsDefaultBeanWithTimezone (0L ));
273
319
assertEquals (aposToQuotes ("{'date':'1970-01-01X01:00:00'}" ), json );
274
320
}
321
+
322
+
323
+
324
+ private static Date judate (int year , int month , int day , int hour , int minutes , int seconds , int millis , String tz ) {
325
+ Calendar cal = Calendar .getInstance ();
326
+ cal .set (year , month -1 , day , hour , minutes , seconds );
327
+ cal .set (Calendar .MILLISECOND , millis );
328
+ cal .setTimeZone (TimeZone .getTimeZone (tz ));
329
+
330
+ return cal .getTime ();
331
+ }
332
+
333
+ private void serialize (ObjectMapper mapper , Object date , String expected ) throws IOException {
334
+ String actual = mapper .writeValueAsString (date );
335
+ Assert .assertEquals (quote (expected ), actual );
336
+ }
275
337
}
0 commit comments