Top Description Inners Fields Constructors Methods
org.apache.commons.lang3.time

public Class DurationFormatUtils

extends Object
Class Inheritance
Imports
java.text.SimpleDateFormat, java.util.ArrayList, .Calendar, .Date, .GregorianCalendar, .Objects, .TimeZone, java.util.stream.Stream, org.apache.commons.lang3.StringUtils, .Strings, .Validate

Duration formatting utilities and constants. The following table describes the tokens used in the pattern language for formatting.
Pattern Tokens
characterduration element
yyears
Mmonths
ddays
Hhours
mminutes
sseconds
Smilliseconds
'text'arbitrary text content
Note: It's not currently possible to include a single-quote in a format.
Token values are printed using decimal digits. A token character can be repeated to ensure that the field occupies a certain minimum size. Values will be left-padded with 0 unless padding is disabled in the method invocation.
Tokens can be marked as optional by surrounding them with brackets [ ]. These tokens will only be printed if the token value is non-zero. Literals within optional blocks will only be printed if the preceding non-literal token is non-zero. Leading optional literals will only be printed if the following non-literal is non-zero. Multiple optional blocks can be used to group literals with the desired token.

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)


Example Output
patternDuration.ofDays(1)Duration.ofHours(1)Duration.ofMinutes(1)Duration.ZERO
d'd'H'h'm'm's's'1d0h0m0s0d1h0m0s0d0h1m0s0d0h0m0s
d'd'[H'h'm'm']s's'1d0s0d1h0s0d1m0s0d0s
[d'd'H'h'm'm']s's'1d0s1h0s1m0s0s
[d'd'H'h'm'm's's']1d1h1m
['{'d'}']HH':'mm{1}00:0001:0000:0100:00
['{'dd'}']['<'HH'>']['('mm')']{01}<01>(00)
[dHms]111
Note: Optional blocks cannot be nested.
Since
2.1

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
DurationFormatUtils.Token

Element that is parsed from the format pattern.

Field Summary

Modifier and TypeField and Description
pack-priv static final String
d

pack-priv static final String
H

private static final int
public static final String
ISO_EXTENDED_FORMAT_PATTERN

Pattern used with FastDateFormat and SimpleDateFormat for the ISO 8601 period format used in durations.

pack-priv static final String
M

pack-priv static final String
m

private static final int
pack-priv static final String
s

pack-priv static final String
S

private static final int
pack-priv static final String
y

Constructor Summary

AccessConstructor and Description
public
DurationFormatUtils()
Deprecated TODO Make private in 4.0.

DurationFormatUtils instances should NOT be constructed in standard programming.

Method Summary

Modifier and TypeMethod and Description
pack-priv static String

Returns:

the formatted string
format
(final DurationFormatUtils.Token[]
the tokens
tokens
,
final long
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 null
formatDuration
(final long
the 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 null
formatDuration
(final long
the 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 null
formatDurationHMS
(final long
the duration to format
durationMillis
)

Formats the time gap as a string.

public static String

Returns:

the formatted duration, not null
formatDurationISO
(final long
the duration to format
durationMillis
)

Formats the time gap as a string.

public static String

Returns:

the formatted text in days/hours/minutes/seconds, not null
formatDurationWords
(final long
the 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 null
formatPeriod
(final long
the 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 null
formatPeriod
(final long
the 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 null
formatPeriodISO
(final long
the 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.Token[]

Returns:

array of Token[]
lexx
(final String
the format to parse, not null
format
)

Parses a classic date format string into Tokens

private static String

Returns:

the string result
paddedValue
(final long
the value to convert
value
,
final boolean
whether to pad with zeroes
padWithZeros
,
final int
the size to pad to (ignored if padWithZeros is false)
count
)

Converts a long to a String with optional zero padding.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

dback to summary
pack-priv static final String d
Hback to summary
pack-priv static final String H
HOURS_PER_DAYback to summary
private static final int HOURS_PER_DAY
ISO_EXTENDED_FORMAT_PATTERNback to summary
public static final String ISO_EXTENDED_FORMAT_PATTERN

Pattern used with FastDateFormat and SimpleDateFormat for the ISO 8601 period format used in durations.

See Also
org.apache.commons.lang3.time.FastDateFormat, java.text.SimpleDateFormat
Mback to summary
pack-priv static final String M
mback to summary
pack-priv static final String m
MINUTES_PER_HOURback to summary
private static final int MINUTES_PER_HOUR
sback to summary
pack-priv static final String s
Sback to summary
pack-priv static final String S
SECONDS_PER_MINUTESback to summary
private static final int SECONDS_PER_MINUTES
yback to summary
pack-priv static final String y

Constructor Detail

DurationFormatUtilsback 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.

Annotations
@Deprecated

Method Detail

formatback to summary
pack-priv static String format(final DurationFormatUtils.Token[] tokens, final long years, final long months, final long days, final long hours, final long minutes, final long seconds, final long milliseconds, final boolean padWithZeros)

The internal method to do the formatting.

Parameters
tokens:DurationFormatUtils.Token[]

the tokens

years:long

the number of years

months:long

the number of months

days:long

the number of days

hours:long

the number of hours

minutes:long

the number of minutes

seconds:long

the number of seconds

milliseconds:long

the number of millis

padWithZeros:boolean

whether to pad

Returns:String

the formatted string

formatDurationback 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.

Parameters
durationMillis:long

the duration to format

format:String

the way in which to format the duration, not null

Returns:String

the formatted duration, not null

Exceptions
IllegalArgumentException:
if durationMillis is negative
formatDurationback 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.

Parameters
durationMillis:long

the duration to format

format:String

the way in which to format the duration, not null

padWithZeros:boolean

whether to pad the left-hand side side of numbers with 0's

Returns:String

the formatted duration, not null

Exceptions
IllegalArgumentException:
if durationMillis is negative
formatDurationHMSback to summary
public static String formatDurationHMS(final long durationMillis)

Formats the time gap as a string.

The format used is ISO 8601-like: HH:mm:ss.SSS.

Parameters
durationMillis:long

the duration to format

Returns:String

the formatted duration, not null

Exceptions
IllegalArgumentException:
if durationMillis is negative
formatDurationISOback 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.

Parameters
durationMillis:long

the duration to format

Returns:String

the formatted duration, not null

Exceptions
IllegalArgumentException:
if durationMillis is negative
formatDurationWordsback 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.

Parameters
durationMillis:long

the elapsed time to report in milliseconds

suppressLeadingZeroElements:boolean

suppresses leading 0 elements

suppressTrailingZeroElements:boolean

suppresses trailing 0 elements

Returns:String

the formatted text in days/hours/minutes/seconds, not null

Exceptions
IllegalArgumentException:
if durationMillis is negative
formatPeriodback 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.

Parameters
startMillis:long

the start of the duration

endMillis:long

the end of the duration

format:String

the way in which to format the duration, not null

Returns:String

the formatted duration, not null

Exceptions
IllegalArgumentException:
if startMillis is greater than endMillis
formatPeriodback 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.

Parameters
startMillis:long

the start of the duration

endMillis:long

the end of the duration

format:String

the way in which to format the duration, not null

padWithZeros:boolean

whether to pad the left-hand side side of numbers with 0's

timezone:TimeZone

the millis are defined in

Returns:String

the formatted duration, not null

Exceptions
IllegalArgumentException:
if startMillis is greater than endMillis
formatPeriodISOback 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.

Parameters
startMillis:long

the start of the duration to format

endMillis:long

the end of the duration to format

Returns:String

the formatted duration, not null

Exceptions
IllegalArgumentException:
if startMillis is greater than endMillis
lexxback to summary
pack-priv static DurationFormatUtils.Token[] lexx(final String format)

Parses a classic date format string into Tokens

Parameters
format:String

the format to parse, not null

Returns:DurationFormatUtils.Token[]

array of Token[]

paddedValueback to summary
private static String paddedValue(final long value, final boolean padWithZeros, final int count)

Converts a long to a String with optional zero padding.

Parameters
value:long

the value to convert

padWithZeros:boolean

whether to pad with zeroes

count:int

the size to pad to (ignored if padWithZeros is false)

Returns:String

the string result

org.apache.commons.lang3.time back to summary

pack-priv final Class DurationFormatUtils.Token

extends Object
Class Inheritance

Element that is parsed from the format pattern.

Field Summary

Modifier and TypeField and Description
private int
private static final DurationFormatUtils.Token[]
EMPTY_ARRAY

Empty array.

private int
private final CharSequence

Constructor Summary

AccessConstructor 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.

Method Summary

Modifier and TypeMethod and Description
pack-priv static boolean

Returns:

boolean true if contained
containsTokenWithValue
(final DurationFormatUtils.Token[]
set to look in
tokens
,
final Object
to look for
value
)

Helper method to determine if a set of tokens contain a value

public boolean

Returns:

boolean true if equal
equals
(final Object
Object to consider equality of
obj2
)

Overrides java.lang.Object.equals.

Supports equality of this Token to another Token.

pack-priv int

Returns:

int number of values represented
getCount
()

Gets the current number of values represented

pack-priv Object

Returns:

Object value, non-null.
getValue
()

Gets the particular value this token represents.

public int

Returns:

The hash code for the token
hashCode
()

Overrides java.lang.Object.hashCode.

Returns a hash code for the token equal to the hash code for the token's value.

pack-priv void
increment()

Adds another one of the value

public String

Returns:

String representation of the token
toString
()

Overrides java.lang.Object.toString.

Represents this token as a String.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

countback to summary
private int count
EMPTY_ARRAYback to summary
private static final DurationFormatUtils.Token[] EMPTY_ARRAY

Empty array.

optionalIndexback to summary
private int optionalIndex
valueback to summary
private final CharSequence value

Constructor Detail

Tokenback 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'.

Parameters
value:CharSequence

value to wrap, non-null.

optional:boolean

whether the token is optional

optionalIndex:int

the index of the optional token within the pattern

Method Detail

containsTokenWithValueback to summary
pack-priv static boolean containsTokenWithValue(final DurationFormatUtils.Token[] tokens, final Object value)

Helper method to determine if a set of tokens contain a value

Parameters
tokens:DurationFormatUtils.Token[]

set to look in

value:Object

to look for

Returns:boolean

boolean true if contained

equalsback to summary
public boolean equals(final Object obj2)

Overrides java.lang.Object.equals.

Supports equality of this Token to another Token.

Parameters
obj2:Object

Object to consider equality of

Returns:boolean

boolean true if equal

Annotations
@Override
getCountback to summary
pack-priv int getCount()

Gets the current number of values represented

Returns:int

int number of values represented

getValueback to summary
pack-priv Object getValue()

Gets the particular value this token represents.

Returns:Object

Object value, non-null.

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

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.

Returns:int

The hash code for the token

Annotations
@Override
incrementback to summary
pack-priv void increment()

Adds another one of the value

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Represents this token as a String.

Returns:String

String representation of the token

Annotations
@Override