1
1
package com .fasterxml .jackson .datatype .joda ;
2
2
3
3
import java .io .IOException ;
4
+ import java .util .TimeZone ;
4
5
5
6
import org .joda .time .DateMidnight ;
7
+ import org .joda .time .DateTimeZone ;
6
8
7
9
import com .fasterxml .jackson .annotation .JsonFormat ;
8
10
import com .fasterxml .jackson .annotation .JsonTypeInfo ;
@@ -29,12 +31,51 @@ public AlternateFormat(DateMidnight v) {
29
31
}
30
32
}
31
33
34
+ static class FormattedDateMidnight {
35
+ @ JsonFormat (timezone ="EST" )
36
+ public DateMidnight dateMidnight ;
37
+ }
38
+
32
39
/*
33
40
/**********************************************************
34
41
/* Test methods
35
42
/**********************************************************
36
43
*/
37
44
45
+ public void testDateMidnightDeserWithTimeZone () throws IOException
46
+ {
47
+ MAPPER .setTimeZone (TimeZone .getTimeZone ("Europe/Paris" ));
48
+ // couple of acceptable formats, so:
49
+ DateMidnight date = MAPPER .readValue ("[2001,5,25]" , DateMidnight .class );
50
+ assertEquals (2001 , date .getYear ());
51
+ assertEquals (5 , date .getMonthOfYear ());
52
+ assertEquals (25 , date .getDayOfMonth ());
53
+
54
+ DateMidnight date2 = MAPPER .readValue (quote ("2005-07-13" ), DateMidnight .class );
55
+ assertEquals (2005 , date2 .getYear ());
56
+ assertEquals (7 , date2 .getMonthOfYear ());
57
+ assertEquals (13 , date2 .getDayOfMonth ());
58
+
59
+ // since 1.6.1, for [JACKSON-360]
60
+ assertNull (MAPPER .readValue (quote ("" ), DateMidnight .class ));
61
+
62
+
63
+ MAPPER .setTimeZone (TimeZone .getTimeZone ("America/Los_Angeles" ));
64
+ // couple of acceptable formats, so:
65
+ date = MAPPER .readValue ("[2001,5,25]" , DateMidnight .class );
66
+ assertEquals (2001 , date .getYear ());
67
+ assertEquals (5 , date .getMonthOfYear ());
68
+ assertEquals (25 , date .getDayOfMonth ());
69
+
70
+ date2 = MAPPER .readValue (quote ("2005-07-13" ), DateMidnight .class );
71
+ assertEquals (2005 , date2 .getYear ());
72
+ assertEquals (7 , date2 .getMonthOfYear ());
73
+ assertEquals (13 , date2 .getDayOfMonth ());
74
+
75
+ // since 1.6.1, for [JACKSON-360]
76
+ assertNull (MAPPER .readValue (quote ("" ), DateMidnight .class ));
77
+ }
78
+
38
79
public void testDateMidnightDeser () throws IOException
39
80
{
40
81
// couple of acceptable formats, so:
@@ -85,4 +126,27 @@ public void testCustomFormat() throws Exception
85
126
assertNotNull (output .value );
86
127
assertEquals (inputDate , output .value );
87
128
}
129
+
130
+ public void testWithTimeZoneOverride () throws Exception
131
+ {
132
+ ObjectMapper mapper = jodaMapper ();
133
+
134
+ DateMidnight date = mapper .readValue ("[2001,5,25]" , DateMidnight .class );
135
+ FormattedDateMidnight input = new FormattedDateMidnight ();
136
+ input .dateMidnight = date ;
137
+ String json = mapper .writeValueAsString (input );
138
+
139
+ FormattedDateMidnight result = mapper .readValue (json , FormattedDateMidnight .class );
140
+ assertNotNull (result );
141
+
142
+ // Ensure timezone sticks:
143
+ DateMidnight resultMidnight = result .dateMidnight ;
144
+ assertEquals (2001 , resultMidnight .getYear ());
145
+ assertEquals (5 , resultMidnight .getMonthOfYear ());
146
+ assertEquals (25 , resultMidnight .getDayOfMonth ());
147
+
148
+ DateTimeZone resultTz = resultMidnight .getZone ();
149
+ // Is this stable enough for testing?
150
+ assertEquals ("America/New_York" , resultTz .getID ());
151
+ }
88
152
}
0 commit comments