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 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   * TestCase for DateFormatUtils.
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         // format UTC
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     public void testLang312() {
226         String pattern = "dd/MM/yyyy";
227         String expected = "19/04/1948";
228         TimeZone timeZone = TimeZone.getTimeZone("CET");
229         Locale locale = Locale.GERMANY;
230 
231         // show Calendar is good
232         Calendar cal = Calendar.getInstance(timeZone, locale);
233         cal.set(1948, 3, 19);
234         assertEquals(expected, DateFormatUtils.format( cal.getTime(), pattern, timeZone, locale ) );
235 
236         Date date = new Date(48, 3, 19);
237 
238         // test JDK
239         java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(pattern, locale);
240         sdf.setTimeZone(timeZone);
241 // There's nothing we can do if the JDK fails, so just going to pring a warning in this case
242 //        assertEquals(expected, sdf.format( date ) );
243         if( ! expected.equals( sdf.format( date ) ) ) {
244             System.out.println("WARNING: JDK test failed - testLang312()");
245         }
246 
247         // test Commons
248         assertEquals(expected, DateFormatUtils.format( date, pattern, timeZone, locale ) );
249     }
250     */
251 
252 }