LANG-404 Add Calendar flavour format methods to DateFormatUtils

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@615243 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Niall Pemberton 2008-01-25 15:20:32 +00:00
parent 680d605d3f
commit e266303c56
3 changed files with 82 additions and 0 deletions

View File

@ -97,3 +97,4 @@ IMPROVEMENTS IN 2.4:
* [LANG-362] - Add ExtendedMessageFormat to org.apache.commons.lang.text
* [LANG-366] - add MultiFormat
* [LANG-371] - ToStringStyle javadoc should show examples of styles
* [LANG-404] - Add Calendar flavour format methods to DateFormatUtils

View File

@ -16,6 +16,7 @@
*/
package org.apache.commons.lang.time;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
@ -183,6 +184,19 @@ public class DateFormatUtils {
public static String format(Date date, String pattern) {
return format(date, pattern, null, null);
}
/**
* <p>Formats a calendar into a specific pattern.</p>
*
* @param calendar the calendar to format
* @param pattern the pattern to use to format the calendar
* @return the formatted calendar
* @see FastDateFormat#format(Calendar)
* @since 2.4
*/
public static String format(Calendar calendar, String pattern) {
return format(calendar, pattern, null, null);
}
/**
* <p>Formats a date/time into a specific pattern in a time zone.</p>
@ -208,6 +222,20 @@ public class DateFormatUtils {
return format(date, pattern, timeZone, null);
}
/**
* <p>Formats a calendar into a specific pattern in a time zone.</p>
*
* @param calendar the calendar to format
* @param pattern the pattern to use to format the calendar
* @param timeZone the time zone to use, may be <code>null</code>
* @return the formatted calendar
* @see FastDateFormat#format(Calendar)
* @since 2.4
*/
public static String format(Calendar calendar, String pattern, TimeZone timeZone) {
return format(calendar, pattern, timeZone, null);
}
/**
* <p>Formats a date/time into a specific pattern in a locale.</p>
*
@ -232,6 +260,20 @@ public class DateFormatUtils {
return format(date, pattern, null, locale);
}
/**
* <p>Formats a calendar into a specific pattern in a locale.</p>
*
* @param calendar the calendar to format
* @param pattern the pattern to use to format the calendar
* @param locale the locale to use, may be <code>null</code>
* @return the formatted calendar
* @see FastDateFormat#format(Calendar)
* @since 2.4
*/
public static String format(Calendar calendar, String pattern, Locale locale) {
return format(calendar, pattern, null, locale);
}
/**
* <p>Formats a date/time into a specific pattern in a time zone and locale.</p>
*
@ -259,4 +301,20 @@ public class DateFormatUtils {
return df.format(date);
}
/**
* <p>Formats a calendar into a specific pattern in a time zone and locale.</p>
*
* @param calendar the calendar to format
* @param pattern the pattern to use to format the calendar
* @param timeZone the time zone to use, may be <code>null</code>
* @param locale the locale to use, may be <code>null</code>
* @return the formatted calendar
* @see FastDateFormat#format(Calendar)
* @since 2.4
*/
public static String format(Calendar calendar, String pattern, TimeZone timeZone, Locale locale) {
FastDateFormat df = FastDateFormat.getInstance(pattern, timeZone, locale);
return df.format(calendar);
}
}

View File

@ -85,6 +85,29 @@ public class DateFormatUtilsTest extends TestCase {
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);