Java TimeUnit Usage millisToReadableTime(long milliseconds)

Here you can find the source of millisToReadableTime(long milliseconds)

Description

millis To Readable Time

License

Open Source License

Declaration

public static final String millisToReadableTime(long milliseconds) 

Method Source Code


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

import java.util.concurrent.TimeUnit;

public class Main {
    public static final String millisToReadableTime(long milliseconds) {
        long millis = milliseconds;
        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 sb = new StringBuilder();

        if (days > 1) {
            sb.append(days);//from w  ww.j  ava2 s . c om
            sb.append(" days");
        }
        if (hours > 1) {
            if (days > 1) {
                sb.append(", ");
            }
            sb.append(hours);
            sb.append(" hours");
        }
        if (minutes > 1) {
            if (hours > 1 || days > 1) {
                sb.append(", ");
            }
            sb.append(minutes);
            sb.append(" minutes");
        }
        if (TimeUnit.MILLISECONDS.toMinutes(milliseconds) < 2) {
            sb.append(seconds);
            sb.append(" seconds");
        }

        return sb.toString();
    }
}

Related

  1. millisBetweenNanoTimes(long startNanos, long endNanos)
  2. millisecondsToHumanReadable(long duration)
  3. millisElapsedSince(long startNanoTime)
  4. millisToDays(long millisLocal)
  5. millisToHMSms(long millis)
  6. millisToString(double millis)
  7. millisToString(long millis)
  8. millisToString(long ms)
  9. millisToStringConvert(long millis)