Java TimeUnit Usage getDurationBreakdown(long millis)

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

Description

Convert a millisecond duration to a string format

License

Apache License

Parameter

Parameter Description
millis A duration to convert to a string form

Return

A string of the form "X Days Y Hours Z Minutes A Seconds".

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 {
    /**/*from  w w  w .  ja  v a2 s  . c  om*/
     * Convert a millisecond duration to a string format
     *
     * @param millis A duration to convert to a string form
     * @return A string of the form "X Days Y Hours Z Minutes A Seconds".
     */
    public static String getDurationBreakdown(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.MILLISECONDS.toMillis(minutes);

        StringBuilder sb = new StringBuilder(64);
        if (days > 0)
            sb.append(days).append(" days ");
        if (hours > 0)
            sb.append(hours).append(" hours ");
        if (minutes > 0)
            sb.append(minutes).append(" minutes ");
        if (seconds > 0)
            sb.append(seconds).append(" seconds ");
        if (millis > 0)
            sb.append(millis).append(" millis");

        return sb.toString().trim();
    }
}

Related

  1. getDays(long endTs, Long lookback)
  2. getDeltaBetweenTimestamps(final long start, final long end)
  3. getDifferenceDays(Date d1, Date d2)
  4. getDiffInMinutes(Calendar startTime, Calendar endTime)
  5. getDurationBreakdown(long millis)
  6. getDurationBreakdown(long millis)
  7. getDurationBreakdown(long millis)
  8. getDurationBreakdown(long millis)
  9. getDurationFromMillis(long millis)