Java TimeUnit Usage millisToTimeDelta(long millis)

Here you can find the source of millisToTimeDelta(long millis)

Description

millis To Time Delta

License

Open Source License

Declaration

public static String millisToTimeDelta(long millis) 

Method Source Code


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

import java.util.concurrent.TimeUnit;

public class Main {
    public static String millisToTimeDelta(long millis) {
        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);
        millis -= TimeUnit.SECONDS.toMillis(seconds);
        String out = "";
        if (days > 0) {
            out += days + "d ";
        }//ww  w.j  a v a  2  s  .  c  o  m
        if (hours > 0) {
            out += hours + "h ";
        }
        if (minutes > 0) {
            out += minutes + "m ";
        }
        if (seconds > 0) {
            out += seconds + "s ";
        }
        if (millis > 0) {
            out += millis + "ms";
        }
        return out.trim();
    }
}

Related

  1. millisToString(double millis)
  2. millisToString(long millis)
  3. millisToString(long ms)
  4. millisToStringConvert(long millis)
  5. millisToTime(long millis)
  6. min()
  7. minutesBetween(Date date1, Date date2)
  8. minutesSince(final long startNanos)
  9. nanoElapseToHumanReadable(final Long nanoElapse)