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 number)
format Duration
long hrs = number / HRS;
number = number % HRS;
long mins = number / MINS;
number = number % MINS;
long secs = number / SECS;
number = number % SECS;
return hrs + ":" + mins + ":" + secs + "." + number;
StringformatDuration(long seconds)
Formats a given duration in seconds (e.g.
String format = "";
if (seconds <= 0L) {
    return "";
long hours = seconds / 3600;
long minutes = (seconds % 3600) / 60;
long secs = (seconds % 60);
format += hours > 0 ? String.format("%02d", hours) + "h " : "";
...
StringformatDuration(long seconds)
Formats a duration in seconds as a string of the form "3d 04:12:56".
if (seconds > 0L) {
    long lRest;
    String strDays = formatDurationLpad(String.valueOf(seconds / DAY)) + "d ";
    lRest = seconds % DAY;
    String strHours = formatDurationLpad(String.valueOf(lRest / HOUR)) + ":";
    lRest %= HOUR;
    String strMinutes = formatDurationLpad(String.valueOf(lRest / MINUTE)) + ":";
    lRest %= MINUTE;
...
StringformatDuration(long t_millis)
format Duration
long t_seconds = (t_millis / (1000)) % 60;
long t_minutes = (t_millis / (1000 * 60)) % 60;
long t_hours = (t_millis / (1000 * 60 * 60)) % 24;
long t_days = (t_millis / (1000 * 60 * 60 * 24));
return t_days + "d " + t_hours + "h " + t_minutes + "m " + t_seconds + "s";
StringformatDuration(long timeInMillis)
format Duration
return formatDuration(toDuration(timeInMillis));
StringformatDurationAsTime(final int duration)
Formats the specified number of seconds as a string containing the number of hours, minutes and seconds.
final StringBuilder result = new StringBuilder();
final int hours = duration / 3600;
final int minutes = duration / 60 % 60;
final int seconds = duration % 60;
if (hours > 0) {
    appendTime(result, hours).append(':');
appendTime(result, minutes).append(':');
...
StringformatDurationHMSms(long ms)
format Duration HM Sms
long hrs = ms / 1000 / 60 / 60;
long min = ms / 1000 / 60 % 60;
long sec = ms / 1000 % 60;
long rms = ms % 1000;
return leftPad(String.valueOf(hrs), 2, '0') + ":" + leftPad(String.valueOf(min), 2, '0') + ":"
        + leftPad(String.valueOf(sec), 2, '0') + ":" + leftPad(String.valueOf(rms), 3, '0');
StringformatDurationLpad(final String s)
Leftpad the string with "0", if the string is only one character long.
return (s.length() == 1 ? "0" + s : s);
StringformatDurationMills(long millis)
Returns a formated string representation of millis .
long seconds = millis / 1000;
long absSeconds = Math.abs(seconds);
String positive = String.format("%d:%02d:%02d.%03d", absSeconds / 3600, (absSeconds % 3600) / 60,
        absSeconds % 60, millis % 1000);
return seconds < 0 ? "-" + positive : positive;
StringformatDurationOneUnit(long milis)
Formats length of time periods in a nice format
long secs = milis / 1000l;
if (secs < 2l) {
    return "1 second";
if (secs == 2l) {
    return "2 seconds";
long value = secs;
...