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

StringformatHuman(Duration duration)
format Human
return formatHuman(duration, false);
StringformatISO8601Duration(int[] t)
format ISO8601 duration string, for example [1,0,0,1,0,0,0] → P1YT1H
StringBuilder result = new StringBuilder(24);
result.append('P');
if (t[0] != 0)
    result.append(t[0]).append('Y');
if (t[1] != 0)
    result.append(t[1]).append('M');
if (t[2] != 0)
    result.append(t[2]).append('D');
...
StringformatNanoDuration(final long nanos)
Formats the given number of nanoseconds to a time like #h/m/s/ms/us/ns
if (nanos >= 3600000000000L) {
    return String.format("%.0fh", (nanos / 3600000000000.0D)); 
} else if (nanos >= 60000000000L) {
    return String.format("%.0fm", (nanos / 60000000000.0D)); 
} else if (nanos >= 1000000000L) {
    return String.format("%.0fs", (nanos / 1000000000.0D)); 
} else if (nanos >= 1000000L) {
    return String.format("%.0fms", (nanos / 1000000.0D)); 
...
StringformatPeriod(final Period period, final boolean wrapInDurationFunction)
format Period
final long totalMonths = period.toTotalMonths();
if (totalMonths == 0) {
    if (wrapInDurationFunction) {
        return "duration( \"P0M\" )";
    } else {
        return "P0M";
final StringBuilder sb = new StringBuilder();
if (wrapInDurationFunction) {
    sb.append("duration( \"");
if (totalMonths < 0) {
    sb.append("-P");
} else {
    sb.append('P');
final long years = Math.abs(totalMonths / 12);
if (years != 0) {
    sb.append(years).append('Y');
final long months = Math.abs(totalMonths % 12);
if (months != 0) {
    sb.append(months).append('M');
if (wrapInDurationFunction) {
    sb.append("\" )");
return sb.toString();
StringformatSeconds(Duration duration)
format Seconds
return SECONDS_FORMAT.format(duration.toMillis() / 1000d);
StringformatShortDuration(long ms)
format Short Duration
long hour = ms / HOUR_TO_MILLISEC;
long min = (ms - hour * HOUR_TO_MILLISEC) / MIN_TO_MILLISEC;
return String.format("%sh %sm", hour, min);
DurationformattedStringToDuration(final String formattedDuration)
Converts a formatted String into a Duration object.
if (formattedDuration == null) {
    return null;
final TemporalAccessor ta = DateTimeFormatter.ofPattern(DURATION_FORMAT).parse(formattedDuration.trim());
return Duration.between(LocalTime.MIDNIGHT, LocalTime.from(ta));
StringformatTime(long duration)
format Time
if (duration == Long.MAX_VALUE)
    return "LIVE";
long seconds = Math.round(duration / 1000.0);
long hours = seconds / (60 * 60);
seconds %= 60 * 60;
long minutes = seconds / 60;
seconds %= 60;
return (hours > 0 ? hours + ":" : "") + (minutes < 10 ? "0" + minutes : minutes) + ":"
...
StringformatTime(long duration)
format Time
return String.format("%d.%03d", duration / 1000, duration % 1000);
StringformatTimeDuration(long timeDuration)
format Time Duration
StringBuilder builder = new StringBuilder();
long hours = timeDuration / (60 * 60 * 1000);
long mod = (timeDuration % (60 * 60 * 1000));
long minutes = mod / (60 * 1000);
mod = mod % (60 * 1000);
long seconds = mod / 1000;
mod = mod % 1000;
long ms = mod;
...