blob: e4dd00db83c4166810638b0dc41bb7a86552a098 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.lang.time;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;
import junit.framework.TestCase;
/**
* TestCase for DateFormatUtils.
*
* @author Apache Software Foundation
* @author Apache Ant - DateUtilsTest
* @author <a href="mailto:sbailliez@apache.org">Stephane Bailliez</a>
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
*/
public class DateFormatUtilsTest extends TestCase {
public DateFormatUtilsTest(String s) {
super(s);
}
//-----------------------------------------------------------------------
public void testConstructor() {
assertNotNull(new DateFormatUtils());
Constructor[] cons = DateFormatUtils.class.getDeclaredConstructors();
assertEquals(1, cons.length);
assertEquals(true, Modifier.isPublic(cons[0].getModifiers()));
assertEquals(true, Modifier.isPublic(DateFormatUtils.class.getModifiers()));
assertEquals(false, Modifier.isFinal(DateFormatUtils.class.getModifiers()));
}
//-----------------------------------------------------------------------
public void testFormat() {
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
c.set(2005,0,1,12,0,0);
c.setTimeZone(TimeZone.getDefault());
StringBuffer buffer = new StringBuffer ();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1;
int day = c.get(Calendar.DAY_OF_MONTH);
int hour = c.get(Calendar.HOUR_OF_DAY);
buffer.append (year);
buffer.append(month);
buffer.append(day);
buffer.append(hour);
assertEquals(buffer.toString(), DateFormatUtils.format(c.getTime(), "yyyyMdH"));
assertEquals(buffer.toString(), DateFormatUtils.format(c.getTime().getTime(), "yyyyMdH"));
assertEquals(buffer.toString(), DateFormatUtils.format(c.getTime(), "yyyyMdH", Locale.US));
assertEquals(buffer.toString(), DateFormatUtils.format(c.getTime().getTime(), "yyyyMdH", Locale.US));
}
//-----------------------------------------------------------------------
public void testFormatCalendar() {
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
c.set(2005,0,1,12,0,0);
c.setTimeZone(TimeZone.getDefault());
StringBuffer buffer = new StringBuffer ();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1;
int day = c.get(Calendar.DAY_OF_MONTH);
int hour = c.get(Calendar.HOUR_OF_DAY);
buffer.append (year);
buffer.append(month);
buffer.append(day);
buffer.append(hour);
assertEquals(buffer.toString(), DateFormatUtils.format(c, "yyyyMdH"));
assertEquals(buffer.toString(), DateFormatUtils.format(c.getTime(), "yyyyMdH"));
assertEquals(buffer.toString(), DateFormatUtils.format(c, "yyyyMdH", Locale.US));
assertEquals(buffer.toString(), DateFormatUtils.format(c.getTime(), "yyyyMdH", Locale.US));
}
public void testFormatUTC() {
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
c.set(2005,0,1,12,0,0);
assertEquals ("2005-01-01T12:00:00", DateFormatUtils.formatUTC(c.getTime(), DateFormatUtils.ISO_DATETIME_FORMAT.getPattern()));
assertEquals ("2005-01-01T12:00:00", DateFormatUtils.formatUTC(c.getTime().getTime(), DateFormatUtils.ISO_DATETIME_FORMAT.getPattern()));
assertEquals ("2005-01-01T12:00:00", DateFormatUtils.formatUTC(c.getTime(), DateFormatUtils.ISO_DATETIME_FORMAT.getPattern(), Locale.US));
assertEquals ("2005-01-01T12:00:00", DateFormatUtils.formatUTC(c.getTime().getTime(), DateFormatUtils.ISO_DATETIME_FORMAT.getPattern(), Locale.US));
}
public void testDateTimeISO(){
TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
Calendar cal = Calendar.getInstance(timeZone);
cal.set(2002,1,23,9,11,12);
String text = DateFormatUtils.format(cal.getTime(),
DateFormatUtils.ISO_DATETIME_FORMAT.getPattern(), timeZone);
assertEquals("2002-02-23T09:11:12", text);
text = DateFormatUtils.format(cal.getTime().getTime(),
DateFormatUtils.ISO_DATETIME_FORMAT.getPattern(), timeZone);
assertEquals("2002-02-23T09:11:12", text);
text = DateFormatUtils.ISO_DATETIME_FORMAT.format(cal);
assertEquals("2002-02-23T09:11:12", text);
text = DateFormatUtils.format(cal.getTime(),
DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern(), timeZone);
assertEquals("2002-02-23T09:11:12-03:00", text);
text = DateFormatUtils.format(cal.getTime().getTime(),
DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern(), timeZone);
assertEquals("2002-02-23T09:11:12-03:00", text);
text = DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(cal);
assertEquals("2002-02-23T09:11:12-03:00", text);
}
public void testDateISO(){
TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
Calendar cal = Calendar.getInstance(timeZone);
cal.set(2002,1,23,10,11,12);
String text = DateFormatUtils.format(cal.getTime(),
DateFormatUtils.ISO_DATE_FORMAT.getPattern(), timeZone);
assertEquals("2002-02-23", text);
text = DateFormatUtils.format(cal.getTime().getTime(),
DateFormatUtils.ISO_DATE_FORMAT.getPattern(), timeZone);
assertEquals("2002-02-23", text);
text = DateFormatUtils.ISO_DATE_FORMAT.format(cal);
assertEquals("2002-02-23", text);
text = DateFormatUtils.format(cal.getTime(),
DateFormatUtils.ISO_DATE_TIME_ZONE_FORMAT.getPattern(), timeZone);
assertEquals("2002-02-23-03:00", text);
text = DateFormatUtils.format(cal.getTime().getTime(),
DateFormatUtils.ISO_DATE_TIME_ZONE_FORMAT.getPattern(), timeZone);
assertEquals("2002-02-23-03:00", text);
text = DateFormatUtils.ISO_DATE_TIME_ZONE_FORMAT.format(cal);
assertEquals("2002-02-23-03:00", text);
}
public void testTimeISO(){
TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
Calendar cal = Calendar.getInstance(timeZone);
cal.set(2002,1,23,10,11,12);
String text = DateFormatUtils.format(cal.getTime(),
DateFormatUtils.ISO_TIME_FORMAT.getPattern(), timeZone);
assertEquals("T10:11:12", text);
text = DateFormatUtils.format(cal.getTime().getTime(),
DateFormatUtils.ISO_TIME_FORMAT.getPattern(), timeZone);
assertEquals("T10:11:12", text);
text = DateFormatUtils.ISO_TIME_FORMAT.format(cal);
assertEquals("T10:11:12", text);
text = DateFormatUtils.format(cal.getTime(),
DateFormatUtils.ISO_TIME_TIME_ZONE_FORMAT.getPattern(), timeZone);
assertEquals("T10:11:12-03:00", text);
text = DateFormatUtils.format(cal.getTime().getTime(),
DateFormatUtils.ISO_TIME_TIME_ZONE_FORMAT.getPattern(), timeZone);
assertEquals("T10:11:12-03:00", text);
text = DateFormatUtils.ISO_TIME_TIME_ZONE_FORMAT.format(cal);
assertEquals("T10:11:12-03:00", text);
}
public void testTimeNoTISO(){
TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
Calendar cal = Calendar.getInstance(timeZone);
cal.set(2002,1,23,10,11,12);
String text = DateFormatUtils.format(cal.getTime(),
DateFormatUtils.ISO_TIME_NO_T_FORMAT.getPattern(), timeZone);
assertEquals("10:11:12", text);
text = DateFormatUtils.format(cal.getTime().getTime(),
DateFormatUtils.ISO_TIME_NO_T_FORMAT.getPattern(), timeZone);
assertEquals("10:11:12", text);
text = DateFormatUtils.ISO_TIME_NO_T_FORMAT.format(cal);
assertEquals("10:11:12", text);
text = DateFormatUtils.format(cal.getTime(),
DateFormatUtils.ISO_TIME_NO_T_TIME_ZONE_FORMAT.getPattern(), timeZone);
assertEquals("10:11:12-03:00", text);
text = DateFormatUtils.format(cal.getTime().getTime(),
DateFormatUtils.ISO_TIME_NO_T_TIME_ZONE_FORMAT.getPattern(), timeZone);
assertEquals("10:11:12-03:00", text);
text = DateFormatUtils.ISO_TIME_NO_T_TIME_ZONE_FORMAT.format(cal);
assertEquals("10:11:12-03:00", text);
}
public void testSMTP(){
TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
Calendar cal = Calendar.getInstance(timeZone);
cal.set(2003,5,8,10,11,12);
String text = DateFormatUtils.format(cal.getTime(),
DateFormatUtils.SMTP_DATETIME_FORMAT.getPattern(), timeZone,
DateFormatUtils.SMTP_DATETIME_FORMAT.getLocale());
assertEquals("Sun, 08 Jun 2003 10:11:12 -0300", text);
text = DateFormatUtils.format(cal.getTime().getTime(),
DateFormatUtils.SMTP_DATETIME_FORMAT.getPattern(), timeZone,
DateFormatUtils.SMTP_DATETIME_FORMAT.getLocale());
assertEquals("Sun, 08 Jun 2003 10:11:12 -0300", text);
text = DateFormatUtils.SMTP_DATETIME_FORMAT.format(cal);
assertEquals("Sun, 08 Jun 2003 10:11:12 -0300", text);
// format UTC
text = DateFormatUtils.formatUTC(cal.getTime().getTime(),
DateFormatUtils.SMTP_DATETIME_FORMAT.getPattern(),
DateFormatUtils.SMTP_DATETIME_FORMAT.getLocale());
assertEquals("Sun, 08 Jun 2003 13:11:12 +0000", text);
}
/*
public void testLang312() {
String pattern = "dd/MM/yyyy";
String expected = "19/04/1948";
TimeZone timeZone = TimeZone.getTimeZone("CET");
Locale locale = Locale.GERMANY;
// show Calendar is good
Calendar cal = Calendar.getInstance(timeZone, locale);
cal.set(1948, 3, 19);
assertEquals(expected, DateFormatUtils.format( cal.getTime(), pattern, timeZone, locale ) );
Date date = new Date(48, 3, 19);
// test JDK
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(pattern, locale);
sdf.setTimeZone(timeZone);
// There's nothing we can do if the JDK fails, so just going to pring a warning in this case
// assertEquals(expected, sdf.format( date ) );
if( ! expected.equals( sdf.format( date ) ) ) {
System.out.println("WARNING: JDK test failed - testLang312()");
}
// test Commons
assertEquals(expected, DateFormatUtils.format( date, pattern, timeZone, locale ) );
}
*/
}