Package org.kiwiproject.time
Class KiwiDurationFormatters
java.lang.Object
org.kiwiproject.time.KiwiDurationFormatters
Utilities for formatting durations of various types.
See Apache Commons DurationFormatUtils for even more formatter methods.
- Implementation Note:
dropwizard-utilmust be a dependency or code using this class won't compile; this requirement will be necessary until the deprecated methods are removed.
-
Method Summary
Modifier and TypeMethodDescriptionstatic StringformatDropwizardDurationWords(io.dropwizard.util.Duration duration) Formats a DropwizardDurationusing English words.static StringformatDurationWords(long durationMillis) Deprecated, for removal: This API element is subject to removal in a future version.static StringformatDurationWords(io.dropwizard.util.Duration duration) Deprecated, for removal: This API element is subject to removal in a future version.for removal; replaced byformatDropwizardDurationWords(Duration)static StringformatDurationWords(Duration duration) Deprecated, for removal: This API element is subject to removal in a future version.for removal; replaced byformatJavaDurationWords(java.time.Duration)static StringformatJavaDurationWords(Duration duration) Formats a JavaDurationusing English words.static StringformatMillisecondDurationWords(long durationMillis) A thin wrapper aroundDurationFormatUtils.formatDurationWords(long, boolean, boolean)that always suppresses leading and trailing "zero elements" because why would you want to see "0 days 7 hours 25 minutes 0 seconds" instead of just "7 hours 25 minutes"?static StringformatNanosecondDurationWords(long durationNanos) A thin wrapper aroundDurationFormatUtils.formatDurationWords(long, boolean, boolean)that always suppresses leading and trailing "zero elements" because why would you want to see "0 days 7 hours 25 minutes 0 seconds" instead of just "7 hours 25 minutes"?
-
Method Details
-
formatDurationWords
@Deprecated(since="4.6.0", forRemoval=true) @KiwiDeprecated(replacedBy="formatJavaDurationWords(java.time.Duration duration)", usageSeverity=SEVERE, removeAt="5.0.0") public static String formatDurationWords(Duration duration) Deprecated, for removal: This API element is subject to removal in a future version.for removal; replaced byformatJavaDurationWords(java.time.Duration)Formats a JavaDurationusing English words. Converts the duration to millis and then callsformatDurationWords(long).- Parameters:
duration- the Java duration to format- Returns:
- the duration in words (e.g., 2 hours 5 minutes)
-
formatJavaDurationWords
Formats a JavaDurationusing English words. Converts the duration to nanos and then callsformatNanosecondDurationWords(long).If the duration is null, the literal string
"null"is returned. If the duration is zero,"0 seconds"is returned.- Parameters:
duration- the Java duration to format- Returns:
- the duration in words (e.g., 2 hours 5 minutes)
- Throws:
IllegalArgumentException- if the duration is negative- See Also:
-
formatDurationWords
@Deprecated(since="4.6.0", forRemoval=true) @KiwiDeprecated(replacedBy="formatDropwizardDurationWords(io.dropwizard.util.Duration duration)", usageSeverity=SEVERE, removeAt="5.0.0") public static String formatDurationWords(io.dropwizard.util.Duration duration) Deprecated, for removal: This API element is subject to removal in a future version.for removal; replaced byformatDropwizardDurationWords(Duration)Formats a Dropwizardio.dropwizard.util.Durationusing English words. Converts the duration to millis and then callsformatDurationWords(long).- Parameters:
duration- the Dropwizard duration to format- Returns:
- the duration in words (e.g., 1 minute 45 seconds)
- Implementation Note:
- You will need the Dropwizard Duration class available at runtime to call this method!
-
formatDropwizardDurationWords
Formats a DropwizardDurationusing English words. Converts the duration to nanos and then callsformatNanosecondDurationWords(long).If the duration is null, the literal string
"null"is returned. If the duration is zero,"0 seconds"is returned.- Parameters:
duration- the Dropwizard duration to format- Returns:
- the duration in words (e.g., 1 minute 45 seconds)
- Throws:
IllegalArgumentException- if the duration is negative- See Also:
- Implementation Note:
- You will need the Dropwizard Duration class available at runtime to call this method!
-
formatDurationWords
@Deprecated(since="4.6.0", forRemoval=true) @KiwiDeprecated(replacedBy="formatMillisecondDurationWords(long durationMillis)", usageSeverity=SEVERE, removeAt="5.0.0") public static String formatDurationWords(long durationMillis) Deprecated, for removal: This API element is subject to removal in a future version.for removal; replaced byformatMillisecondDurationWords(long)A thin wrapper aroundDurationFormatUtils.formatDurationWords(long, boolean, boolean)that always suppresses leading and trailing "zero elements" because why would you want to see "0 days 7 hours 25 minutes 0 seconds" instead of just "7 hours 25 minutes"? (We cannot think of a good reason...)- Parameters:
durationMillis- the duration in milliseconds to format- Returns:
- the duration in words (e.g., 10 minutes)
- See Also:
-
DurationFormatUtils.formatDurationWords(long, boolean, boolean)
- Implementation Note:
- The only real reason for this method to exist is, so we don't constantly have to pass the two boolean arguments. Plus, boolean arguments are evil because what exactly does "true, false" tell you without requiring you to look at the parameter documentation?
-
formatMillisecondDurationWords
A thin wrapper aroundDurationFormatUtils.formatDurationWords(long, boolean, boolean)that always suppresses leading and trailing "zero elements" because why would you want to see "0 days 7 hours 25 minutes 0 seconds" instead of just "7 hours 25 minutes"? (We cannot think of a good reason...).Unlike
DurationFormatUtils, it also handles durations less than one second down to 1 nanosecond.If the duration is zero,
"0 milliseconds"is returned.If you need to handle durations less than a millisecond, use
formatNanosecondDurationWords(long)- Parameters:
durationMillis- the duration in milliseconds to format- Returns:
- the duration in words (e.g., 10 minutes)
- Throws:
IllegalArgumentException- if the duration is negative- See Also:
-
DurationFormatUtils.formatDurationWords(long, boolean, boolean)formatNanosecondDurationWords(long)
- Implementation Note:
- The main reasons for this method to exist are so that we don't constantly have to pass the two
boolean arguments to
DurationFormatUtilsand so that we can handle durations less than one second. Plus, boolean arguments are evil because what exactly does "true, false" tell you without requiring you to look at the parameter documentation?
-
formatNanosecondDurationWords
A thin wrapper aroundDurationFormatUtils.formatDurationWords(long, boolean, boolean)that always suppresses leading and trailing "zero elements" because why would you want to see "0 days 7 hours 25 minutes 0 seconds" instead of just "7 hours 25 minutes"? (We cannot think of a good reason...)If the duration is zero,
"0 nanosecond"is returned.Unlike
DurationFormatUtils, it also handles durations less than one second down to 1 nanosecond.- Parameters:
durationNanos- the duration in nanoseconds to format- Returns:
- the duration in words (e.g., 10 minutes, 50 milliseconds)
- Throws:
IllegalArgumentException- if the duration is negative- See Also:
-
DurationFormatUtils.formatDurationWords(long, boolean, boolean)
- Implementation Note:
- The main reasons for this method to exist are so that we don't constantly have to pass the two
boolean arguments to
DurationFormatUtilsand so that we can handle durations less than one second. Plus, boolean arguments are evil because what exactly does "true, false" tell you without requiring you to look at the parameter documentation?
-
formatMillisecondDurationWords(long)