Java TimeUnit Usage millisToString(long ms)

Here you can find the source of millisToString(long ms)

Description

millis To String

License

LGPL

Declaration

public static final String millisToString(long ms) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.util.concurrent.TimeUnit;

public class Main {
    public static final String millisToString(long ms) {
        if (ms <= 0) {
            throw new IllegalArgumentException("Input must be greater than zero");
        }/* ww w .  ja  va 2s .c om*/
        long hours = TimeUnit.MILLISECONDS.toHours(ms);
        ms -= TimeUnit.HOURS.toMillis(hours);
        long minutes = TimeUnit.MILLISECONDS.toMinutes(ms);
        ms -= TimeUnit.MINUTES.toMillis(minutes);
        long seconds = TimeUnit.MILLISECONDS.toSeconds(ms);

        StringBuilder sb = new StringBuilder();
        if (hours > 0) {
            sb.append(hours + "h");
        }
        if (minutes > 0) {
            sb.append(minutes + "m");
        }
        if (seconds > 0) {
            sb.append(seconds + "s");
        }
        return sb.toString();
    }
}

Related

  1. millisToDays(long millisLocal)
  2. millisToHMSms(long millis)
  3. millisToReadableTime(long milliseconds)
  4. millisToString(double millis)
  5. millisToString(long millis)
  6. millisToStringConvert(long millis)
  7. millisToTime(long millis)
  8. millisToTimeDelta(long millis)
  9. min()