Author: niallp
Date: Fri Jan 25 07:20:32 2008
New Revision: 615243
URL: http://svn.apache.org/viewvc?rev=615243&view=rev
Log:
LANG-404 Add Calendar flavour format methods to DateFormatUtils
Modified:
commons/proper/lang/trunk/RELEASE-NOTES.txt
commons/proper/lang/trunk/src/java/org/apache/commons/lang/time/DateFormatUtils.java
commons/proper/lang/trunk/src/test/org/apache/commons/lang/time/DateFormatUtilsTest.java
Modified: commons/proper/lang/trunk/RELEASE-NOTES.txt
URL:
http://svn.apache.org/viewvc/commons/proper/lang/trunk/RELEASE-NOTES.txt?rev=615243&r1=615242&r2=615243&view=diff
==============================================================================
--- commons/proper/lang/trunk/RELEASE-NOTES.txt (original)
+++ commons/proper/lang/trunk/RELEASE-NOTES.txt Fri Jan 25 07:20:32 2008
@@ -97,3 +97,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
\ No newline at end of file
Modified:
commons/proper/lang/trunk/src/java/org/apache/commons/lang/time/DateFormatUtils.java
URL:
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/time/DateFormatUtils.java?rev=615243&r1=615242&r2=615243&view=diff
==============================================================================
---
commons/proper/lang/trunk/src/java/org/apache/commons/lang/time/DateFormatUtils.java
(original)
+++
commons/proper/lang/trunk/src/java/org/apache/commons/lang/time/DateFormatUtils.java
Fri Jan 25 07:20:32 2008
@@ -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 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>
@@ -209,6 +223,20 @@
}
/**
+ * <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>
*
* @param millis the date to format expressed in milliseconds
@@ -233,6 +261,20 @@
}
/**
+ * <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>
*
* @param millis the date to format expressed in milliseconds
@@ -257,6 +299,22 @@
public static String format(Date date, String pattern, TimeZone timeZone,
Locale locale) {
FastDateFormat df = FastDateFormat.getInstance(pattern, timeZone,
locale);
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);
}
}
Modified:
commons/proper/lang/trunk/src/test/org/apache/commons/lang/time/DateFormatUtilsTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/org/apache/commons/lang/time/DateFormatUtilsTest.java?rev=615243&r1=615242&r2=615243&view=diff
==============================================================================
---
commons/proper/lang/trunk/src/test/org/apache/commons/lang/time/DateFormatUtilsTest.java
(original)
+++
commons/proper/lang/trunk/src/test/org/apache/commons/lang/time/DateFormatUtilsTest.java
Fri Jan 25 07:20:32 2008
@@ -85,6 +85,29 @@
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);