public class DurationFormatUtil extends Object
Duration formatting utilities and constants. The following table describes the tokens used in the pattern language for formatting.
character | duration element |
---|---|
y | years |
M | months |
d | days |
H | hours |
m | minutes |
s | seconds |
S | milliseconds |
Modifier and Type | Field and Description |
---|---|
static String |
ISO_EXTENDED_FORMAT_PATTERN
Pattern used with
FastDateFormat and SimpleDateFormat
for the ISO8601 period format used in durations. |
static long |
MILLIS_PER_DAY
Number of milliseconds in a standard day.
|
static long |
MILLIS_PER_HOUR
Number of milliseconds in a standard hour.
|
static long |
MILLIS_PER_MINUTE
Number of milliseconds in a standard minute.
|
static long |
MILLIS_PER_SECOND
Number of milliseconds in a standard second.
|
Constructor and Description |
---|
DurationFormatUtil()
DurationFormatUtils instances should NOT be constructed in standard programming.
|
Modifier and Type | Method and Description |
---|---|
static String |
formatDuration(long durationMillis,
String format)
Formats the time gap as a string, using the specified format, and
using the default timezone.
|
static String |
formatDurationHMS(long durationMillis)
Formats the time gap as a string.
|
static String |
formatDurationISO(long durationMillis)
Formats the time gap as a string.
|
static String |
formatDurationWords(long durationMillis,
boolean suppressLeadingZeroElements,
boolean suppressTrailingZeroElements)
Formats an elapsed time into a plurialization correct string.
|
static String |
formatPeriod(long startMillis,
long endMillis,
String format)
Formats the time gap as a string, using the specified format.
|
static String |
formatPeriod(long startMillis,
long endMillis,
String format,
TimeZone timezone)
Formats the time gap as a string, using the specified format, and
the timezone may be specified.
|
static String |
formatPeriodISO(long startMillis,
long endMillis)
Formats the time gap as a string.
|
public static final long MILLIS_PER_SECOND
public static final long MILLIS_PER_MINUTE
public static final long MILLIS_PER_HOUR
public static final long MILLIS_PER_DAY
public static final String ISO_EXTENDED_FORMAT_PATTERN
Pattern used with FastDateFormat
and SimpleDateFormat
for the ISO8601 period format used in durations.
SimpleDateFormat
,
Constant Field Valuespublic DurationFormatUtil()
DurationFormatUtils instances should NOT be constructed in standard programming.
This constructor is public to permit tools that require a JavaBean instance to operate.
public static String formatDurationHMS(long durationMillis)
Formats the time gap as a string.
The format used is ISO8601-like: H:m:s.S.
durationMillis
- the duration to formatpublic static String formatDurationISO(long durationMillis)
Formats the time gap as a string.
The format used is the ISO8601 period format.
This method formats durations using the days and lower fields of the ISO format pattern, such as P7D6TH5M4.321S.
durationMillis
- the duration to formatpublic static String formatDuration(long durationMillis, String format)
Formats the time gap as a string, using the specified format, and using the default timezone.
This method formats durations using the days and lower fields of the format pattern. Months and larger are not used.
durationMillis
- the duration to formatformat
- the way in which to format the durationpublic static String formatDurationWords(long durationMillis, boolean suppressLeadingZeroElements, boolean suppressTrailingZeroElements)
Formats an elapsed time into a plurialization correct string.
This method formats durations using the days and lower fields of the format pattern. Months and larger are not used.
durationMillis
- the elapsed time to report in millisecondssuppressLeadingZeroElements
- suppresses leading 0 elementssuppressTrailingZeroElements
- suppresses trailing 0 elementspublic static String formatPeriodISO(long startMillis, long endMillis)
Formats the time gap as a string.
The format used is the ISO8601 period format.
startMillis
- the start of the duration to formatendMillis
- the end of the duration to formatpublic static String formatPeriod(long startMillis, long endMillis, String format)
Formats the time gap as a string, using the specified format.
startMillis
- the start of the durationendMillis
- the end of the durationformat
- the way in which to format the durationpublic static String formatPeriod(long startMillis, long endMillis, String format, TimeZone timezone)
Formats the time gap as a string, using the specified format, and the timezone 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.
startMillis
- the start of the durationendMillis
- the end of the durationformat
- the way in which to format the durationtimezone
- the millis are defined inelasticsearch-river-jdbc