View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.lang3.time;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertNotNull;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.lang.reflect.Constructor;
25  import java.lang.reflect.Modifier;
26  import java.text.ParseException;
27  import java.util.Calendar;
28  import java.util.Date;
29  import java.util.Locale;
30  import java.util.TimeZone;
31  
32  import org.apache.commons.lang3.AbstractLangTest;
33  import org.junit.jupiter.api.Test;
34  import org.junitpioneer.jupiter.DefaultLocale;
35  import org.junitpioneer.jupiter.DefaultTimeZone;
36  
37  /**
38   * TestCase for DateFormatUtils.
39   */
40  @SuppressWarnings("deprecation") // tests lots of deprecated items
41  public class DateFormatUtilsTest extends AbstractLangTest {
42      private void assertFormats(final String expectedValue, final String pattern, final TimeZone timeZone, final Calendar cal) {
43          assertEquals(expectedValue, DateFormatUtils.format(cal.getTime(), pattern, timeZone));
44          assertEquals(expectedValue, DateFormatUtils.format(cal.getTime().getTime(), pattern, timeZone));
45          assertEquals(expectedValue, DateFormatUtils.format(cal, pattern, timeZone));
46      }
47  
48      private Calendar createFebruaryTestDate(final TimeZone timeZone) {
49          final Calendar cal = Calendar.getInstance(timeZone);
50          cal.set(2002, Calendar.FEBRUARY, 23, 9, 11, 12);
51          return cal;
52      }
53  
54      private Calendar createJuneTestDate(final TimeZone timeZone) {
55          final Calendar cal = Calendar.getInstance(timeZone);
56          cal.set(2003, Calendar.JUNE, 8, 10, 11, 12);
57          return cal;
58      }
59  
60      @Test
61      public void testConstructor() {
62          assertNotNull(new DateFormatUtils());
63          final Constructor<?>[] cons = DateFormatUtils.class.getDeclaredConstructors();
64          assertEquals(1, cons.length);
65          assertTrue(Modifier.isPublic(cons[0].getModifiers()));
66          assertTrue(Modifier.isPublic(DateFormatUtils.class.getModifiers()));
67          assertFalse(Modifier.isFinal(DateFormatUtils.class.getModifiers()));
68      }
69  
70      @Test
71      public void testDateISO() {
72          testGmtMinus3("2002-02-23", DateFormatUtils.ISO_DATE_FORMAT.getPattern());
73          testGmtMinus3("2002-02-23-03:00", DateFormatUtils.ISO_DATE_TIME_ZONE_FORMAT.getPattern());
74          testUTC("2002-02-23Z", DateFormatUtils.ISO_DATE_TIME_ZONE_FORMAT.getPattern());
75      }
76  
77      @Test
78      public void testDateTimeISO() {
79          testGmtMinus3("2002-02-23T09:11:12", DateFormatUtils.ISO_DATETIME_FORMAT.getPattern());
80          testGmtMinus3("2002-02-23T09:11:12-03:00", DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern());
81          testUTC("2002-02-23T09:11:12Z", DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern());
82      }
83  
84      @Test
85      public void testFormat() {
86          final Calendar c = Calendar.getInstance(FastTimeZone.getGmtTimeZone());
87          c.set(2005, Calendar.JANUARY, 1, 12, 0, 0);
88          c.setTimeZone(TimeZone.getDefault());
89          final StringBuilder buffer = new StringBuilder ();
90          final int year = c.get(Calendar.YEAR);
91          final int month = c.get(Calendar.MONTH) + 1;
92          final int day = c.get(Calendar.DAY_OF_MONTH);
93          final int hour = c.get(Calendar.HOUR_OF_DAY);
94          buffer.append (year);
95          buffer.append(month);
96          buffer.append(day);
97          buffer.append(hour);
98          assertEquals(buffer.toString(), DateFormatUtils.format(c.getTime(), "yyyyMdH"));
99  
100         assertEquals(buffer.toString(), DateFormatUtils.format(c.getTime().getTime(), "yyyyMdH"));
101 
102         assertEquals(buffer.toString(), DateFormatUtils.format(c.getTime(), "yyyyMdH", Locale.US));
103 
104         assertEquals(buffer.toString(), DateFormatUtils.format(c.getTime().getTime(), "yyyyMdH", Locale.US));
105     }
106 
107     @Test
108     public void testFormatCalendar() {
109         final Calendar c = Calendar.getInstance(FastTimeZone.getGmtTimeZone());
110         c.set(2005, Calendar.JANUARY, 1, 12, 0, 0);
111         c.setTimeZone(TimeZone.getDefault());
112         final StringBuilder buffer = new StringBuilder ();
113         final int year = c.get(Calendar.YEAR);
114         final int month = c.get(Calendar.MONTH) + 1;
115         final int day = c.get(Calendar.DAY_OF_MONTH);
116         final int hour = c.get(Calendar.HOUR_OF_DAY);
117         buffer.append (year);
118         buffer.append(month);
119         buffer.append(day);
120         buffer.append(hour);
121         assertEquals(buffer.toString(), DateFormatUtils.format(c, "yyyyMdH"));
122 
123         assertEquals(buffer.toString(), DateFormatUtils.format(c.getTime(), "yyyyMdH"));
124 
125         assertEquals(buffer.toString(), DateFormatUtils.format(c, "yyyyMdH", Locale.US));
126 
127         assertEquals(buffer.toString(), DateFormatUtils.format(c.getTime(), "yyyyMdH", Locale.US));
128     }
129 
130     @Test
131     public void testFormatUTC() {
132         final Calendar c = Calendar.getInstance(FastTimeZone.getGmtTimeZone());
133         c.set(2005, Calendar.JANUARY, 1, 12, 0, 0);
134         assertEquals ("2005-01-01T12:00:00", DateFormatUtils.formatUTC(c.getTime(), DateFormatUtils.ISO_DATETIME_FORMAT.getPattern()));
135 
136         assertEquals ("2005-01-01T12:00:00", DateFormatUtils.formatUTC(c.getTime().getTime(), DateFormatUtils.ISO_DATETIME_FORMAT.getPattern()));
137 
138         assertEquals ("2005-01-01T12:00:00", DateFormatUtils.formatUTC(c.getTime(), DateFormatUtils.ISO_DATETIME_FORMAT.getPattern(), Locale.US));
139 
140         assertEquals ("2005-01-01T12:00:00", DateFormatUtils.formatUTC(c.getTime().getTime(), DateFormatUtils.ISO_DATETIME_FORMAT.getPattern(), Locale.US));
141     }
142 
143     private void testGmtMinus3(final String expectedValue, final String pattern) {
144         final TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
145         assertFormats(expectedValue, pattern, timeZone, createFebruaryTestDate(timeZone));
146     }
147 
148     @Test
149     public void testLANG1000() throws Exception {
150         final String date = "2013-11-18T12:48:05Z";
151         DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.parse(date);
152     }
153 
154     @Test
155     public void testLANG1462() {
156         final TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
157         final Calendar calendar = createJuneTestDate(timeZone);
158         assertEquals("20030608101112", DateFormatUtils.format(calendar, "yyyyMMddHHmmss"));
159         calendar.setTimeZone(TimeZone.getTimeZone("JST"));
160         assertEquals("20030608221112", DateFormatUtils.format(calendar, "yyyyMMddHHmmss"));
161     }
162 
163     @DefaultTimeZone("UTC")
164     @Test
165     public void testLang530() throws ParseException {
166         final Date d = new Date();
167         final String isoDateStr = DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(d);
168         final Date d2 = DateUtils.parseDate(isoDateStr, DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern());
169         // the format loses milliseconds so have to reintroduce them
170         assertEquals(d.getTime(), d2.getTime() + d.getTime() % 1000, "Date not equal to itself ISO formatted and parsed");
171     }
172 
173     /**
174      * According to LANG-916 (https://issues.apache.org/jira/browse/LANG-916),
175      * the format method did contain a bug: it did not use the TimeZone data.
176      *
177      * This method test that the bug is fixed.
178      */
179     @Test
180     public void testLang916() {
181 
182         final Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Paris"));
183         cal.clear();
184         cal.set(2009, 9, 16, 8, 42, 16);
185 
186         // Long.
187         {
188             final String value = DateFormatUtils.format(cal.getTimeInMillis(), DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern(), TimeZone.getTimeZone("Europe/Paris"));
189             assertEquals("2009-10-16T08:42:16+02:00", value, "long");
190         }
191         {
192             final String value = DateFormatUtils.format(cal.getTimeInMillis(), DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern(), TimeZone.getTimeZone("Asia/Kolkata"));
193             assertEquals("2009-10-16T12:12:16+05:30", value, "long");
194         }
195         {
196             final String value = DateFormatUtils.format(cal.getTimeInMillis(), DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern(), TimeZone.getTimeZone("Europe/London"));
197             assertEquals("2009-10-16T07:42:16+01:00", value, "long");
198         }
199 
200         // Calendar.
201         {
202             final String value = DateFormatUtils.format(cal, DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern(), TimeZone.getTimeZone("Europe/Paris"));
203             assertEquals("2009-10-16T08:42:16+02:00", value, "calendar");
204         }
205         {
206             final String value = DateFormatUtils.format(cal, DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern(), TimeZone.getTimeZone("Asia/Kolkata"));
207             assertEquals("2009-10-16T12:12:16+05:30", value, "calendar");
208         }
209         {
210             final String value = DateFormatUtils.format(cal, DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern(), TimeZone.getTimeZone("Europe/London"));
211             assertEquals("2009-10-16T07:42:16+01:00", value, "calendar");
212         }
213     }
214 
215     @DefaultLocale(language = "en")
216     @Test
217     public void testSMTP() {
218         TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
219         Calendar june = createJuneTestDate(timeZone);
220 
221         assertFormats("Sun, 08 Jun 2003 10:11:12 -0300", DateFormatUtils.SMTP_DATETIME_FORMAT.getPattern(),
222                 timeZone, june);
223 
224         timeZone = FastTimeZone.getGmtTimeZone();
225         june = createJuneTestDate(timeZone);
226         assertFormats("Sun, 08 Jun 2003 10:11:12 +0000", DateFormatUtils.SMTP_DATETIME_FORMAT.getPattern(),
227                 timeZone, june);
228     }
229 
230     @Test
231     public void testTimeISO() {
232         testGmtMinus3("T09:11:12", DateFormatUtils.ISO_TIME_FORMAT.getPattern());
233         testGmtMinus3("T09:11:12-03:00", DateFormatUtils.ISO_TIME_TIME_ZONE_FORMAT.getPattern());
234         testUTC("T09:11:12Z", DateFormatUtils.ISO_TIME_TIME_ZONE_FORMAT.getPattern());
235     }
236 
237     @Test
238     public void testTimeNoTISO() {
239         testGmtMinus3("09:11:12", DateFormatUtils.ISO_TIME_NO_T_FORMAT.getPattern());
240         testGmtMinus3("09:11:12-03:00", DateFormatUtils.ISO_TIME_NO_T_TIME_ZONE_FORMAT.getPattern());
241         testUTC("09:11:12Z", DateFormatUtils.ISO_TIME_NO_T_TIME_ZONE_FORMAT.getPattern());
242     }
243 
244     private void testUTC(final String expectedValue, final String pattern) {
245         final TimeZone timeZone = FastTimeZone.getGmtTimeZone();
246         assertFormats(expectedValue, pattern, timeZone, createFebruaryTestDate(timeZone));
247     }
248 }