Java Utililty Methods Duration Format

List of utility methods to do Duration Format

Description

The list of methods to do Duration Format are organized into topic(s).

Method

StringformatDuration(int seconds)
Formats a duration with minutes and seconds, e.g., "93:45"
int minutes = seconds / 60;
int secs = seconds % 60;
StringBuilder builder = new StringBuilder(6);
builder.append(minutes).append(":");
if (secs < 10) {
    builder.append("0");
builder.append(secs);
...
StringformatDuration(long duration)
Formats duration in miliseconds to hh:mm:ss string.
if (duration < 0) {
    return "";
duration /= 1000;
short seconds = (short) (duration % 60);
duration -= seconds;
duration /= 60;
short minutes = (short) (duration % 60);
...
StringformatDuration(long duration)
format Duration
long hourDuration = duration / millisPerHour;
long minuteDuration = (duration % millisPerHour) / millisPerMinute;
long secondDuration = ((duration % millisPerHour) % millisPerMinute) / 1000;
StringBuilder result = new StringBuilder(10);
if (hourDuration > 0) {
    if (hourDuration < 10) {
        result.append('0');
    result.append(hourDuration).append('h');
    if (minuteDuration < 10) {
        result.append('0');
    result.append(minuteDuration).append('m');
} else if (hourDuration <= 0 && minuteDuration > 0) {
    result.append(minuteDuration).append('m');
if (result.length() > 0 && secondDuration < 10) {
    result.append('0');
return result.append(secondDuration).append('s').toString();
StringformatDuration(long duration)
format Duration
long hour = (duration / 3600000);
if (hour == 0)
    return (duration / 60000) + "min " + ((duration % 60000) / 1000) + "s";
long remaining = duration % 3600000;
return hour + "h" + (remaining / 60000) + "min" + ((remaining % 60000) / 1000) + "s";
StringformatDuration(long duration)
format Duration
long ms = duration % 1000;
long s = (duration / ONE_SECOND) % 60;
long m = (duration / ONE_MINUTE) % 60;
long h = (duration / ONE_HOUR) % 24;
long d = duration / ONE_DAY;
String format;
if (d > 0) {
    format = "%d d %02d:%02d h";
...
StringformatDuration(long duration, boolean displayMilliseconds)
Formats a duration in milliseconds.
long remainingDuration = duration;
if (remainingDuration == 0) {
    return displayMilliseconds ? "0 Milliseconds" : "0 Seconds";
StringBuilder result = new StringBuilder();
remainingDuration = processDuration(remainingDuration, MILLIS_IN_ONE_DAY, "Day", result, false);
remainingDuration = processDuration(remainingDuration, MILLIS_IN_ONE_HOUR, "Hour", result, false);
remainingDuration = processDuration(remainingDuration, MILLIS_IN_ONE_MINUTE, "Minute", result, false);
...
StringformatDuration(long duration, boolean simplify, boolean includeMillies)
format Duration
int hours = (int) (duration / 3600000);
duration -= hours * 3600000;
int minutes = (int) (duration / 60000);
duration -= 60000 * minutes;
int seconds = (int) (duration / 1000);
int millis = (int) (duration - seconds * 1000);
if (simplify)
    return formatSimplified(hours, minutes, seconds, millis, includeMillies);
...
StringformatDuration(long durationInMs)
Label for a duration, expressed in numbers of ms, seconds or minutes.
if (durationInMs < 1000) {
    return String.format("%sms", durationInMs);
} else {
    long sec = durationInMs / 1000;
    if (sec < 60) {
        return String.format("%ss", sec);
    } else {
        long min = sec / 60;
...
StringformatDuration(long durationMillis)
format Duration
long[] intervals = new long[] { SECOND, MINUTE, HOUR, DAY };
String[] words = new String[] { " second", " minute", " hour", " day" };
StringBuffer result = new StringBuffer(100);
boolean started = false;
for (int i = intervals.length - 1; i >= 0; i--) {
    long num = durationMillis / intervals[i];
    if (num > 0) {
        if (started) {
...
StringformatDuration(long durationSec)
format Duration
long min = (durationSec + 30L) / 60L;
long hour = min / 60L;
min %= 60L;
if (hour == 0L) {
    return min + smallerFont("mn");
} else {
    return hour + smallerFont("h") + (min < 10 ? "0" : "") + min;