diff --git a/src/java/org/apache/commons/lang/time/DurationFormatUtils.java b/src/java/org/apache/commons/lang/time/DurationFormatUtils.java index 847b38d68..6c7f691c4 100644 --- a/src/java/org/apache/commons/lang/time/DurationFormatUtils.java +++ b/src/java/org/apache/commons/lang/time/DurationFormatUtils.java @@ -60,19 +60,76 @@ * @author Stephane Bailliez * @author Stefan Bodewig * @author Stephen Colebourne + * @author Gary Gregory * @since 2.0 - * @version $Id: DurationFormatUtils.java,v 1.3 2003/07/14 22:25:05 bayard Exp $ + * @version $Id: DurationFormatUtils.java,v 1.4 2003/07/18 17:04:31 ggregory Exp $ */ class DurationFormatUtils { // TODO: Make class public once methods can fully select which fields to output /** - *

DurationFormatUtils instances should NOT be constructed in standard programming.

- * - *

This constructor is public to permit tools that require a JavaBean instance - * to operate.

+ *

Pattern used with FastDateFormat and SimpleDateFormat for the ISO8601 + * date time extended format used in durations.

+ * + * @see org.apache.commons.lang.time.FastDateFormat + * @see java.text.SimpleDateFormat */ - public DurationFormatUtils() { + public static final String ISO_EXTENDED_FORMAT_PATTERN = "'P'yyyy'Y'M'M'd'DT'H'H'm'M's.S'S'"; + + /** + *

ISO8601 formatter for the date time extended format used in durations, + * with XML Schema durations particularly in mind.

+ * + *

This format represents the Gregorian year, month, day, hour, minute, and second components defined + * in § 5.5.3.2 of ISO 8601, respectively. These components are ordered in their significance by their order + * of appearance i.e. as year, month, day, hour, minute, and second.

+ * + *

The ISO8601 extended format PnYnMnDTnHnMnS, where nY + * represents the number of years, nM the number of months, nD the number of days, + * 'T' is the date/time separator, nH the number of hours, nM the number of minutes and + * nS the number of seconds. The number of seconds can include decimal digits to arbitrary precision.

+ * + * @see #ISO_EXTENDED_FORMAT_PATTERN + * @see http://www.w3.org/TR/xmlschema-2/#duration + */ + public static final FastDateFormat ISO_EXTENDED_FORMAT = + FastDateFormat.getInstance(ISO_EXTENDED_FORMAT_PATTERN); + + /** + *

Get the time gap as a string.

+ * + *

The format used is ISO8601-like: + * hours:minutes:seconds.milliseconds.

+ * + * @param millis the duration to format + * @return the time as a String + */ + public static String formatISO(long millis) { + int hours, minutes, seconds, milliseconds; + hours = (int) (millis / DateUtils.MILLIS_IN_HOUR); + millis = millis - (hours * DateUtils.MILLIS_IN_HOUR); + minutes = (int) (millis / DateUtils.MILLIS_IN_MINUTE); + millis = millis - (minutes * DateUtils.MILLIS_IN_MINUTE); + seconds = (int) (millis / DateUtils.MILLIS_IN_SECOND); + millis = millis - (seconds * DateUtils.MILLIS_IN_SECOND); + milliseconds = (int) millis; + + StringBuffer buf = new StringBuffer(32); + buf.append(hours); + buf.append(':'); + buf.append((char) (minutes / 10 + '0')); + buf.append((char) (minutes % 10 + '0')); + buf.append(':'); + buf.append((char) (seconds / 10 + '0')); + buf.append((char) (seconds % 10 + '0')); + buf.append('.'); + if (milliseconds < 10) { + buf.append('0').append('0'); + } else if (milliseconds < 100) { + buf.append('0'); + } + buf.append(milliseconds); + return buf.toString(); } /** @@ -81,8 +138,8 @@ public DurationFormatUtils() { * seconds and has the following behavior.

* *