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(long elapsed)
format Duration
return Duration.ofMillis(elapsed).toString();
StringformatDuration(long elapsedSec)
format Duration
return String.format("%02d:%02d:%02d", elapsedSec / 3600, (elapsedSec % 3600) / 60, elapsedSec % 60);
StringformatDuration(Long input)
format Duration
if (input == null) {
    return "Forever!";
long inputSeconds = input / 1000;
long days = (inputSeconds / (24 * 60 * 60));
long hours = (inputSeconds / (60 * 60)) % 24;
long minutes = (inputSeconds / (60)) % 60;
long seconds = inputSeconds % 60;
...
StringformatDuration(long milis)
Formats length of time periods in a nice format
if (milis < 10 * 1000) {
    return milis / 10 / 100.0 + " seconds";
long seconds = milis / 1000;
long minutes = seconds / 60;
long hours = minutes / 60;
String result = "";
if (hours > 0) {
...
StringformatDuration(long millis)
Formats duration in user friendly format ex: 1d 32m 4ms
long d = millis / DAY_IN_MILLIS;
long h = (millis - d * DAY_IN_MILLIS) / HOUR_IN_MILLIS;
long m = (millis - d * DAY_IN_MILLIS - h * HOUR_IN_MILLIS) / MINUTE_IN_MILLIS;
long s = (millis - d * DAY_IN_MILLIS - h * HOUR_IN_MILLIS - m * MINUTE_IN_MILLIS) / SECOND_IN_MILLIS;
long ms = millis % SECOND_IN_MILLIS;
StringBuilder sb = new StringBuilder();
appendTimePart(d, "d", sb);
appendTimePart(h, "h", sb);
...
StringformatDuration(long millis)
Returns specified duration as a string in the following format: "hours:minutes:seconds.milliseconds".
final int millisInSecond = 1000;
final int millisInMinute = 60 * 1000;
final int millisInHour = 60 * 60 * 1000;
int hours = (int) (millis / millisInHour);
millis = millis - (hours * millisInHour);
int minutes = (int) (millis / millisInMinute);
millis = millis - (minutes * millisInMinute);
int seconds = (int) (millis / millisInSecond);
...
StringformatDuration(long milliseconds)
Format a time duration as "hh:mm:ss"
long seconds = milliseconds / 1000;
long hours = seconds / 3600;
seconds -= hours * 3600;
long minutes = seconds / 60;
seconds -= minutes * 60;
return (hours < 10 ? "0" : "") + hours + ":" + (minutes < 10 ? "0" : "") + minutes + ":"
        + (seconds < 10 ? "0" : "") + seconds;
StringformatDuration(long milliseconds)
format Duration
int s = (int) (milliseconds / 1000);
milliseconds = milliseconds % 1000;
int m = s / 60;
s = s % 60;
int h = m / 60;
m = m % 60;
return padWithLeadingZeros(h, 2) + ":" + padWithLeadingZeros(m, 2) + ":" + padWithLeadingZeros(s, 2) + "."
        + padWithLeadingZeros((int) milliseconds, 3);
...
StringformatDuration(long ms)
format Duration
double nbSec = (double) ms / 1000.0;
return formatDuration(nbSec);
StringformatDuration(long ms)
NOTE: formatDuration2() recommended in most cases for readability
if (ms < 5 * 1000) {
    return ms + "ms";
} else if (ms < 3 * 60 * 1000) {
    return (ms / 1000) + "s";
} else if (ms < 120 * 60 * 1000) {
    return (ms / (60 * 1000)) + "m";
} else if (ms < 3 * 24 * 60 * 60 * 1000) {
    return (ms / (60 * 60 * 1000)) + "h";
...