Java TimeUnit Usage formatDuration(long time)

Here you can find the source of formatDuration(long time)

Description

format Duration

License

Open Source License

Declaration

public static String formatDuration(long time) 

Method Source Code

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

import java.util.concurrent.TimeUnit;

public class Main {
    public static String formatDuration(long time) {
        long millis = time;
        long days = TimeUnit.MILLISECONDS.toDays(millis);
        millis -= TimeUnit.DAYS.toMillis(days);
        long hours = TimeUnit.MILLISECONDS.toHours(millis);
        millis -= TimeUnit.HOURS.toMillis(hours);
        long minutes = TimeUnit.MILLISECONDS.toMinutes(millis);
        millis -= TimeUnit.MINUTES.toMillis(minutes);
        long seconds = TimeUnit.MILLISECONDS.toSeconds(millis);
        StringBuilder b = new StringBuilder();
        if (days > 0)
            b.append(days).append(" Days, ");
        if (hours > 0)
            b.append(hours).append(" Hours, ");
        if (minutes > 0)
            b.append(minutes).append(" Minutes, ");
        return b.append(seconds).append(" Seconds").toString();
    }/*from  w w w.  j av a2  s.  c  o m*/
}

Related

  1. elapsedTimeSince(Date d)
  2. format(long elapsed, boolean hours)
  3. formatDuration(long duration)
  4. formatDuration(long millis)
  5. formatDuration(long millis)
  6. formatDurationSeconds(long seconds)
  7. formatDurationTill(long start)
  8. formatElapsedSecs(long secs)
  9. formatElapsedTime(final long seconds)