Java TimeUnit Usage millisecondsToHumanReadable(long duration)

Here you can find the source of millisecondsToHumanReadable(long duration)

Description

converts time (in milliseconds) to human-readable format "mm:ss"

License

Open Source License

Declaration

public static String millisecondsToHumanReadable(long duration) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.concurrent.TimeUnit;

public class Main {
    /**//from  ww w  .j a  va2s .com
     * converts time (in milliseconds) to human-readable format
     * "<dd:><hh:>mm:ss"
     */
    public static String millisecondsToHumanReadable(long duration) {
        String res;
        long days = TimeUnit.MILLISECONDS.toDays(duration);
        long hours = TimeUnit.MILLISECONDS.toHours(duration)
                - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(duration));
        long minutes = TimeUnit.MILLISECONDS.toMinutes(duration)
                - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(duration));
        long seconds = TimeUnit.MILLISECONDS.toSeconds(duration)
                - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(duration));
        if (days == 0) {
            if (hours == 0) {
                res = String.format("%02d:%02d", minutes, seconds);
            } else {
                res = String.format("%02d:%02d:%02d", hours, minutes, seconds);
            }
        } else {
            res = String.format("%dd%02d:%02d:%02d", days, hours, minutes, seconds);
        }
        return res;
    }
}

Related

  1. julianDayToMillis(int julianDay)
  2. logDuration(org.slf4j.Logger log, String prefix, long startTimeNanos)
  3. logWatchStop(long start, String... params)
  4. mergeResults(List>> futures)
  5. millisBetweenNanoTimes(long startNanos, long endNanos)
  6. millisElapsedSince(long startNanoTime)
  7. millisToDays(long millisLocal)
  8. millisToHMSms(long millis)
  9. millisToReadableTime(long milliseconds)