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(Duration duration)
format Duration
long hours = Math.abs(duration.toHours());
long minutes = Math.abs(duration.minus(duration.toHours(), ChronoUnit.HOURS).toMinutes());
long seconds = Math.abs(duration.minus(duration.toMinutes(), ChronoUnit.MINUTES).getSeconds());
final String f = String.format("%02d:%02d:%02d", hours, minutes, seconds);
return f;
StringformatDuration(Duration duration)
format Duration
long seconds = duration.getSeconds();
long absSeconds = Math.abs(seconds);
String positive = String.format("%d h %d m %d s", absSeconds / 3600, (absSeconds % 3600) / 60,
        absSeconds % 60);
return seconds < 0 ? "-" + positive : positive;
StringformatDuration(Duration duration, boolean inProgress)
format Duration
int hours = (int) duration.toHours();
int minutes = (int) duration.toMinutes() - (hours * 60);
int seconds = (int) (duration.getSeconds() % 60);
if (seconds >= 30) {
    minutes++;
if (minutes >= 60) {
    hours++;
...
StringformatDuration(final Duration duration)
format Duration
long absSeconds = Math.abs(duration.getSeconds());
long seconds = absSeconds % 60;
long minutes = (absSeconds % 3600) / 60;
long hours = absSeconds / 3600;
return (hours == 0 ? "" : hours + ":")
        + (minutes == 0 ? "00" : minutes < 10 ? String.valueOf("0" + minutes) : String.valueOf(minutes))
        + ":"
        + (seconds == 0 ? "00" : seconds < 10 ? String.valueOf("0" + seconds) : String.valueOf(seconds));
...
StringformatDuration(final Duration duration, final boolean wrapInDurationFunction)
format Duration
if (duration.getSeconds() == 0 && duration.getNano() == 0) {
    if (wrapInDurationFunction) {
        return "duration( \"PT0S\" )";
    } else {
        return "PT0S";
final long days = duration.getSeconds() / SECONDS_IN_A_DAY;
...
StringformatDuration(final int duration)
Formats the specified number of seconds as a string containing the number of days, hours, minutes and seconds.
final StringBuilder buff = new StringBuilder();
int seconds = duration;
seconds -= doDuration(buff, seconds, 60 * 60 * 24, "day");
seconds -= doDuration(buff, seconds, 60 * 60, "hour");
seconds -= doDuration(buff, seconds, 60, "minute");
seconds -= doDuration(buff, seconds, 1, "second");
return buff.length() == 0 ? "0 seconds" : buff.toString();
StringformatDuration(final long milliSeconds)
format Duration
final long duration = milliSeconds / 60000;
final long durationHours = duration / 60;
final long durationMinutes = (duration % 60);
final StringBuffer buf = new StringBuffer(10);
buf.append(durationHours);
if (durationMinutes < 10)
    buf.append(":0");
else
...
StringformatDuration(final long seconds)
format Duration
return String.format("%d:%02d:%02d", seconds / 3600, (seconds % 3600) / 60, (seconds % 60));
StringformatDuration(int durationMins)
format Duration
int minutes = durationMins % 60;
int hours = (durationMins - minutes) / 60;
return hours + ":" + String.valueOf(minutes % 60 + 100).substring(1, 3);
StringformatDuration(int seconds)
format Duration
if (seconds == 0) {
    return "";
int days = seconds / 86400;
int hours = (seconds % 86400) / 3600;
int mins = (seconds % 3600) / 60;
int secs = seconds % 60;
StringBuffer buf = new StringBuffer();
...