Java TimeUnit Usage getDurationBreakdown(long millis)

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

Description

get Duration Breakdown

License

Apache License

Parameter

Parameter Description
millis a parameter

Return

String

Declaration

public static String getDurationBreakdown(long millis) 

Method Source Code

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

import java.util.concurrent.TimeUnit;

public class Main {
    /**/* w  ww.  jav a2s  .  c  om*/
     * @param millis
     * @return String
     */
    public static String getDurationBreakdown(long millis) {
        long time = millis;
        if (time < 0) {
            throw new IllegalArgumentException("Duration must be greater than zero!");
        }

        long days = TimeUnit.MILLISECONDS.toDays(time);
        time -= TimeUnit.DAYS.toMillis(days);
        long hours = TimeUnit.MILLISECONDS.toHours(time);
        time -= TimeUnit.HOURS.toMillis(hours);
        long minutes = TimeUnit.MILLISECONDS.toMinutes(time);
        time -= TimeUnit.MINUTES.toMillis(minutes);
        long seconds = TimeUnit.MILLISECONDS.toSeconds(time);

        StringBuilder sb = new StringBuilder(64);
        if (days > 0) {
            sb.append(days);
            sb.append("d");
        }
        if (hours > 0) {
            sb.append(hours);
            sb.append("h");
        }
        if (minutes > 0) {
            sb.append(minutes);
            sb.append("m");
        }
        if (seconds > 0) {
            sb.append(seconds);
            sb.append("s");
        }

        return (sb.toString());
    }
}

Related

  1. getDifferenceDays(Date d1, Date d2)
  2. getDiffInMinutes(Calendar startTime, Calendar endTime)
  3. getDurationBreakdown(long millis)
  4. getDurationBreakdown(long millis)
  5. getDurationBreakdown(long millis)
  6. getDurationBreakdown(long millis)
  7. getDurationFromMillis(long millis)
  8. getDurationFromMilliseconds(long duration)
  9. getDurationFromTwoDates(Date startTime, Date endTime)