1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.lang3.time;
18
19 import org.junit.Test;
20 import static org.junit.Assert.*;
21 import java.lang.reflect.Constructor;
22 import java.lang.reflect.Modifier;
23 import java.util.Calendar;
24 import java.util.Locale;
25 import java.util.TimeZone;
26
27
28
29
30
31 public class DateFormatUtilsTest {
32
33
34 @Test
35 public void testConstructor() {
36 assertNotNull(new DateFormatUtils());
37 final Constructor<?>[] cons = DateFormatUtils.class.getDeclaredConstructors();
38 assertEquals(1, cons.length);
39 assertTrue(Modifier.isPublic(cons[0].getModifiers()));
40 assertTrue(Modifier.isPublic(DateFormatUtils.class.getModifiers()));
41 assertFalse(Modifier.isFinal(DateFormatUtils.class.getModifiers()));
42 }
43
44
45 @Test
46 public void testFormat() {
47 final Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
48 c.set(2005,0,1,12,0,0);
49 c.setTimeZone(TimeZone.getDefault());
50 final StringBuilder buffer = new StringBuilder ();
51 final int year = c.get(Calendar.YEAR);
52 final int month = c.get(Calendar.MONTH) + 1;
53 final int day = c.get(Calendar.DAY_OF_MONTH);
54 final int hour = c.get(Calendar.HOUR_OF_DAY);
55 buffer.append (year);
56 buffer.append(month);
57 buffer.append(day);
58 buffer.append(hour);
59 assertEquals(buffer.toString(), DateFormatUtils.format(c.getTime(), "yyyyMdH"));
60
61 assertEquals(buffer.toString(), DateFormatUtils.format(c.getTime().getTime(), "yyyyMdH"));
62
63 assertEquals(buffer.toString(), DateFormatUtils.format(c.getTime(), "yyyyMdH", Locale.US));
64
65 assertEquals(buffer.toString(), DateFormatUtils.format(c.getTime().getTime(), "yyyyMdH", Locale.US));
66 }
67
68
69 @Test
70 public void testFormatCalendar() {
71 final Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
72 c.set(2005,0,1,12,0,0);
73 c.setTimeZone(TimeZone.getDefault());
74 final StringBuilder buffer = new StringBuilder ();
75 final int year = c.get(Calendar.YEAR);
76 final int month = c.get(Calendar.MONTH) + 1;
77 final int day = c.get(Calendar.DAY_OF_MONTH);
78 final int hour = c.get(Calendar.HOUR_OF_DAY);
79 buffer.append (year);
80 buffer.append(month);
81 buffer.append(day);
82 buffer.append(hour);
83 assertEquals(buffer.toString(), DateFormatUtils.format(c, "yyyyMdH"));
84
85 assertEquals(buffer.toString(), DateFormatUtils.format(c.getTime(), "yyyyMdH"));
86
87 assertEquals(buffer.toString(), DateFormatUtils.format(c, "yyyyMdH", Locale.US));
88
89 assertEquals(buffer.toString(), DateFormatUtils.format(c.getTime(), "yyyyMdH", Locale.US));
90 }
91
92 @Test
93 public void testFormatUTC() {
94 final Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
95 c.set(2005,0,1,12,0,0);
96 assertEquals ("2005-01-01T12:00:00", DateFormatUtils.formatUTC(c.getTime(), DateFormatUtils.ISO_DATETIME_FORMAT.getPattern()));
97
98 assertEquals ("2005-01-01T12:00:00", DateFormatUtils.formatUTC(c.getTime().getTime(), DateFormatUtils.ISO_DATETIME_FORMAT.getPattern()));
99
100 assertEquals ("2005-01-01T12:00:00", DateFormatUtils.formatUTC(c.getTime(), DateFormatUtils.ISO_DATETIME_FORMAT.getPattern(), Locale.US));
101
102 assertEquals ("2005-01-01T12:00:00", DateFormatUtils.formatUTC(c.getTime().getTime(), DateFormatUtils.ISO_DATETIME_FORMAT.getPattern(), Locale.US));
103 }
104
105 @Test
106 public void testDateTimeISO(){
107 final TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
108 final Calendar cal = Calendar.getInstance(timeZone);
109 cal.set(2002,1,23,9,11,12);
110 String text = DateFormatUtils.format(cal.getTime(),
111 DateFormatUtils.ISO_DATETIME_FORMAT.getPattern(), timeZone);
112 assertEquals("2002-02-23T09:11:12", text);
113 text = DateFormatUtils.format(cal.getTime().getTime(),
114 DateFormatUtils.ISO_DATETIME_FORMAT.getPattern(), timeZone);
115 assertEquals("2002-02-23T09:11:12", text);
116 text = DateFormatUtils.ISO_DATETIME_FORMAT.format(cal);
117 assertEquals("2002-02-23T09:11:12", text);
118
119 text = DateFormatUtils.format(cal.getTime(),
120 DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern(), timeZone);
121 assertEquals("2002-02-23T09:11:12-03:00", text);
122 text = DateFormatUtils.format(cal.getTime().getTime(),
123 DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern(), timeZone);
124 assertEquals("2002-02-23T09:11:12-03:00", text);
125 text = DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(cal);
126 assertEquals("2002-02-23T09:11:12-03:00", text);
127 }
128
129 @Test
130 public void testDateISO(){
131 final TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
132 final Calendar cal = Calendar.getInstance(timeZone);
133 cal.set(2002,1,23,10,11,12);
134 String text = DateFormatUtils.format(cal.getTime(),
135 DateFormatUtils.ISO_DATE_FORMAT.getPattern(), timeZone);
136 assertEquals("2002-02-23", text);
137 text = DateFormatUtils.format(cal.getTime().getTime(),
138 DateFormatUtils.ISO_DATE_FORMAT.getPattern(), timeZone);
139 assertEquals("2002-02-23", text);
140 text = DateFormatUtils.ISO_DATE_FORMAT.format(cal);
141 assertEquals("2002-02-23", text);
142
143 text = DateFormatUtils.format(cal.getTime(),
144 DateFormatUtils.ISO_DATE_TIME_ZONE_FORMAT.getPattern(), timeZone);
145 assertEquals("2002-02-23-03:00", text);
146 text = DateFormatUtils.format(cal.getTime().getTime(),
147 DateFormatUtils.ISO_DATE_TIME_ZONE_FORMAT.getPattern(), timeZone);
148 assertEquals("2002-02-23-03:00", text);
149 text = DateFormatUtils.ISO_DATE_TIME_ZONE_FORMAT.format(cal);
150 assertEquals("2002-02-23-03:00", text);
151 }
152
153 @Test
154 public void testTimeISO(){
155 final TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
156 final Calendar cal = Calendar.getInstance(timeZone);
157 cal.set(2002,1,23,10,11,12);
158 String text = DateFormatUtils.format(cal.getTime(),
159 DateFormatUtils.ISO_TIME_FORMAT.getPattern(), timeZone);
160 assertEquals("T10:11:12", text);
161 text = DateFormatUtils.format(cal.getTime().getTime(),
162 DateFormatUtils.ISO_TIME_FORMAT.getPattern(), timeZone);
163 assertEquals("T10:11:12", text);
164 text = DateFormatUtils.ISO_TIME_FORMAT.format(cal);
165 assertEquals("T10:11:12", text);
166
167 text = DateFormatUtils.format(cal.getTime(),
168 DateFormatUtils.ISO_TIME_TIME_ZONE_FORMAT.getPattern(), timeZone);
169 assertEquals("T10:11:12-03:00", text);
170 text = DateFormatUtils.format(cal.getTime().getTime(),
171 DateFormatUtils.ISO_TIME_TIME_ZONE_FORMAT.getPattern(), timeZone);
172 assertEquals("T10:11:12-03:00", text);
173 text = DateFormatUtils.ISO_TIME_TIME_ZONE_FORMAT.format(cal);
174 assertEquals("T10:11:12-03:00", text);
175 }
176
177 @Test
178 public void testTimeNoTISO(){
179 final TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
180 final Calendar cal = Calendar.getInstance(timeZone);
181 cal.set(2002,1,23,10,11,12);
182 String text = DateFormatUtils.format(cal.getTime(),
183 DateFormatUtils.ISO_TIME_NO_T_FORMAT.getPattern(), timeZone);
184 assertEquals("10:11:12", text);
185 text = DateFormatUtils.format(cal.getTime().getTime(),
186 DateFormatUtils.ISO_TIME_NO_T_FORMAT.getPattern(), timeZone);
187 assertEquals("10:11:12", text);
188 text = DateFormatUtils.ISO_TIME_NO_T_FORMAT.format(cal);
189 assertEquals("10:11:12", text);
190
191 text = DateFormatUtils.format(cal.getTime(),
192 DateFormatUtils.ISO_TIME_NO_T_TIME_ZONE_FORMAT.getPattern(), timeZone);
193 assertEquals("10:11:12-03:00", text);
194 text = DateFormatUtils.format(cal.getTime().getTime(),
195 DateFormatUtils.ISO_TIME_NO_T_TIME_ZONE_FORMAT.getPattern(), timeZone);
196 assertEquals("10:11:12-03:00", text);
197 text = DateFormatUtils.ISO_TIME_NO_T_TIME_ZONE_FORMAT.format(cal);
198 assertEquals("10:11:12-03:00", text);
199 }
200
201 @Test
202 public void testSMTP(){
203 final TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
204 final Calendar cal = Calendar.getInstance(timeZone);
205 cal.set(2003,5,8,10,11,12);
206 String text = DateFormatUtils.format(cal.getTime(),
207 DateFormatUtils.SMTP_DATETIME_FORMAT.getPattern(), timeZone,
208 DateFormatUtils.SMTP_DATETIME_FORMAT.getLocale());
209 assertEquals("Sun, 08 Jun 2003 10:11:12 -0300", text);
210 text = DateFormatUtils.format(cal.getTime().getTime(),
211 DateFormatUtils.SMTP_DATETIME_FORMAT.getPattern(), timeZone,
212 DateFormatUtils.SMTP_DATETIME_FORMAT.getLocale());
213 assertEquals("Sun, 08 Jun 2003 10:11:12 -0300", text);
214 text = DateFormatUtils.SMTP_DATETIME_FORMAT.format(cal);
215 assertEquals("Sun, 08 Jun 2003 10:11:12 -0300", text);
216
217
218 text = DateFormatUtils.formatUTC(cal.getTime().getTime(),
219 DateFormatUtils.SMTP_DATETIME_FORMAT.getPattern(),
220 DateFormatUtils.SMTP_DATETIME_FORMAT.getLocale());
221 assertEquals("Sun, 08 Jun 2003 13:11:12 +0000", text);
222 }
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252 }