| character | duration element |
|---|---|
| y | years |
| M | months |
| d | days |
| H | hours |
| m | minutes |
| s | seconds |
| S | milliseconds |
| 'text' | arbitrary text content |
Notes
on Optional Tokens:
Multiple optional tokens without literals can result in impossible to understand output.
Patterns where all tokens are optional can produce empty strings.
(See examples below)
| pattern | Duration.ofDays(1) | Duration.ofHours(1) | Duration.ofMinutes(1) | Duration.ZERO |
|---|---|---|---|---|
| d'd'H'h'm'm's's' | 1d0h0m0s | 0d1h0m0s | 0d0h1m0s | 0d0h0m0s |
| d'd'[H'h'm'm']s's' | 1d0s | 0d1h0s | 0d1m0s | 0d0s |
| [d'd'H'h'm'm']s's' | 1d0s | 1h0s | 1m0s | 0s |
| [d'd'H'h'm'm's's'] | 1d | 1h | 1m | |
| ['{'d'}']HH':'mm | {1}00:00 | 01:00 | 00:01 | 00:00 |
| ['{'dd'}']['<'HH'>']['('mm')'] | {01} | <01> | (00) | |
| [dHms] | 1 | 1 | 1 |
| Modifier and Type | Class and Description |
|---|---|
| pack-priv static class | DurationFormatUtils.
Element that is parsed from the format pattern. |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv static final String | |
| pack-priv static final String | |
| private static final int | |
| public static final String | ISO_EXTENDED_FORMAT_PATTERN
Pattern used with |
| pack-priv static final String | |
| pack-priv static final String | |
| private static final int | |
| pack-priv static final String | |
| pack-priv static final String | |
| private static final int | |
| pack-priv static final String |
| Access | Constructor and Description |
|---|---|
| public | DurationFormatUtils()
Deprecated
TODO Make private in 4.0.
DurationFormatUtils instances should NOT be constructed in standard programming. |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv static String | Returns: the formatted stringthe tokens tokens,the number of years years, final long the number of months months, final long the number of days days, final long the number of hours hours, final long the number of minutes minutes, final long the number of seconds seconds, final long the number of millis milliseconds, final boolean whether to pad padWithZeros)The internal method to do the formatting. |
| public static String | Returns: the formatted duration, not nullthe duration to format durationMillis, final String the way in which to format the duration, not null format)Formats the time gap as a string, using the specified format, and padding with zeros. |
| public static String | Returns: the formatted duration, not nullthe duration to format durationMillis, final String the way in which to format the duration, not null format, final boolean whether to pad the left-hand side side of numbers with 0's padWithZeros)Formats the time gap as a string, using the specified format. |
| public static String | Returns: the formatted duration, not nullthe duration to format durationMillis)Formats the time gap as a string. |
| public static String | Returns: the formatted duration, not nullthe duration to format durationMillis)Formats the time gap as a string. |
| public static String | Returns: the formatted text in days/hours/minutes/seconds, not nullthe elapsed time to report in milliseconds durationMillis, final boolean suppresses leading 0 elements suppressLeadingZeroElements, final boolean suppresses trailing 0 elements suppressTrailingZeroElements)Formats an elapsed time into a pluralization correct string. |
| public static String | Returns: the formatted duration, not nullthe start of the duration startMillis, final long the end of the duration endMillis, final String the way in which to format the duration, not null format)Formats the time gap as a string, using the specified format. |
| public static String | Returns: the formatted duration, not nullthe start of the duration startMillis, final long the end of the duration endMillis, final String the way in which to format the duration, not null format, final boolean whether to pad the left-hand side side of numbers with 0's padWithZeros, final TimeZone the millis are defined in timezone)Formats the time gap as a string, using the specified format. |
| public static String | Returns: the formatted duration, not nullthe start of the duration to format startMillis, final long the end of the duration to format endMillis)Formats the time gap as a string. |
| pack-priv static DurationFormatUtils. | |
| private static String | Returns: the string resultthe value to convert value, final boolean whether to pad with zeroes padWithZeros, final int the size to pad to (ignored if count)padWithZeros is false)Converts a |
| d | back to summary |
|---|---|
| pack-priv static final String d | |
| H | back to summary |
|---|---|
| pack-priv static final String H | |
| HOURS_PER_DAY | back to summary |
|---|---|
| private static final int HOURS_PER_DAY | |
| ISO_EXTENDED_FORMAT_PATTERN | back to summary |
|---|---|
| public static final String ISO_EXTENDED_FORMAT_PATTERN Pattern used with | |
| M | back to summary |
|---|---|
| pack-priv static final String M | |
| m | back to summary |
|---|---|
| pack-priv static final String m | |
| MINUTES_PER_HOUR | back to summary |
|---|---|
| private static final int MINUTES_PER_HOUR | |
| s | back to summary |
|---|---|
| pack-priv static final String s | |
| S | back to summary |
|---|---|
| pack-priv static final String S | |
| SECONDS_PER_MINUTES | back to summary |
|---|---|
| private static final int SECONDS_PER_MINUTES | |
| y | back to summary |
|---|---|
| pack-priv static final String y | |
| DurationFormatUtils | back to summary |
|---|---|
| public DurationFormatUtils()
Deprecated TODO Make private in 4.0. DurationFormatUtils instances should NOT be constructed in standard programming. This constructor is public to permit tools that require a JavaBean instance to operate.
| |
| format | back to summary |
|---|---|
| pack-priv static String format(final DurationFormatUtils. The internal method to do the formatting.
| |
| formatDuration | back to summary |
|---|---|
| public static String formatDuration(final long durationMillis, final String format) Formats the time gap as a string, using the specified format, and padding with zeros. This method formats durations using the days and lower fields of the format pattern. Months and larger are not used.
| |
| formatDuration | back to summary |
|---|---|
| public static String formatDuration(final long durationMillis, final String format, final boolean padWithZeros) Formats the time gap as a string, using the specified format. Padding the left-hand side side of numbers with zeroes is optional. This method formats durations using the days and lower fields of the format pattern. Months and larger are not used.
| |
| formatDurationHMS | back to summary |
|---|---|
| public static String formatDurationHMS(final long durationMillis) Formats the time gap as a string. The format used is ISO 8601-like:
| |
| formatDurationISO | back to summary |
|---|---|
| public static String formatDurationISO(final long durationMillis) Formats the time gap as a string. The format used is the ISO 8601 period format. This method formats durations using the days and lower fields of the ISO format pattern, such as P7D6TH5M4.321S.
| |
| formatDurationWords | back to summary |
|---|---|
| public static String formatDurationWords(final long durationMillis, final boolean suppressLeadingZeroElements, final boolean suppressTrailingZeroElements) Formats an elapsed time into a pluralization correct string. This method formats durations using the days and lower fields of the format pattern. Months and larger are not used.
| |
| formatPeriod | back to summary |
|---|---|
| public static String formatPeriod(final long startMillis, final long endMillis, final String format) Formats the time gap as a string, using the specified format. Padding the left-hand side side of numbers with zeroes is optional.
| |
| formatPeriod | back to summary |
|---|---|
| public static String formatPeriod(final long startMillis, final long endMillis, final String format, final boolean padWithZeros, final TimeZone timezone) Formats the time gap as a string, using the specified format. Padding the left-hand side side of numbers with zeroes is optional and the time zone may be specified. When calculating the difference between months/days, it chooses to calculate months first. So when working out the number of months and days between January 15th and March 10th, it choose 1 month and 23 days gained by choosing January->February = 1 month and then calculating days forwards, and not the 1 month and 26 days gained by choosing March -> February = 1 month and then calculating days backwards. For more control, the Joda-Time library is recommended.
| |
| formatPeriodISO | back to summary |
|---|---|
| public static String formatPeriodISO(final long startMillis, final long endMillis) Formats the time gap as a string. The format used is the ISO 8601 period format.
| |
| lexx | back to summary |
|---|---|
| pack-priv static DurationFormatUtils. Parses a classic date format string into Tokens
| |
| paddedValue | back to summary |
|---|---|
| private static String paddedValue(final long value, final boolean padWithZeros, final int count) Converts a
| |
| Modifier and Type | Field and Description |
|---|---|
| private int | |
| private static final DurationFormatUtils. | EMPTY_ARRAY
Empty array. |
| private int | |
| private final CharSequence |
| Access | Constructor and Description |
|---|---|
| pack-priv | Token(final CharSequence
value to wrap, non-null. value, final boolean whether the token is optional optional, final int the index of the optional token within the pattern optionalIndex)Wraps a token around a value. |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv static boolean | Returns: booleantrue if containedset to look in tokens,to look for value)Helper method to determine if a set of tokens contain a value |
| public boolean | Returns: booleantrue if equalObject to consider equality of obj2)Overrides java. Supports equality of this Token to another Token. |
| pack-priv int | |
| pack-priv Object | |
| public int | Returns: The hash code for the tokenOverrides java. Returns a hash code for the token equal to the hash code for the token's value. |
| pack-priv void | |
| public String | Returns: String representation of the tokenOverrides java. Represents this token as a String. |
| count | back to summary |
|---|---|
| private int count | |
| EMPTY_ARRAY | back to summary |
|---|---|
| private static final DurationFormatUtils. Empty array. | |
| optionalIndex | back to summary |
|---|---|
| private int optionalIndex | |
| value | back to summary |
|---|---|
| private final CharSequence value | |
| Token | back to summary |
|---|---|
| pack-priv Token(final CharSequence value, final boolean optional, final int optionalIndex) Wraps a token around a value. A value would be something like a 'Y'.
| |
| containsTokenWithValue | back to summary |
|---|---|
| pack-priv static boolean containsTokenWithValue(final DurationFormatUtils. Helper method to determine if a set of tokens contain a value
| |
| equals | back to summary |
|---|---|
| public boolean equals(final Object obj2) Overrides java. Supports equality of this Token to another Token. | |
| getCount | back to summary |
|---|---|
| pack-priv int getCount() Gets the current number of values represented
| |
| getValue | back to summary |
|---|---|
| pack-priv Object getValue() Gets the particular value this token represents.
| |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Returns a hash code for the token equal to the hash code for the token's value. Thus 'TT' and 'TTTT' will have the same hash code.
| |
| increment | back to summary |
|---|---|
| pack-priv void increment() Adds another one of the value | |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Represents this token as a String. | |