Java TimeUnit Usage formatDuration(long millis)

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

Description

Duration in the form - PTHH:mm:ss.mmm P is period or duration and T designates a time in ISO8601 format.

License

LGPL

Parameter

Parameter Description
date - created from milliseconds

Return

- duration up to 24hrs max

Declaration

public static String formatDuration(long millis) 

Method Source Code

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

import java.util.concurrent.TimeUnit;

public class Main {
    /**/* ww w  .  ja va  2s.  c  o  m*/
     * Duration in the form - PTHH:mm:ss.mmm
     * P is period or duration and T designates
     * a time in ISO8601 format.
     * 
     * @param date - created from milliseconds
     * @return - duration up to 24hrs max
     */
    public static String formatDuration(long millis) {
        if (millis < 0) {
            throw new IllegalArgumentException(String.format("Duration: %d is < 0", 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);

        return String.format("PT%02d:%02d:%02d.%03d", hours, minutes, seconds, millis);
    }
}

Related

  1. elapsedMicroSec(long startNanoTime)
  2. elapsedTime(long start, long end)
  3. elapsedTimeSince(Date d)
  4. format(long elapsed, boolean hours)
  5. formatDuration(long duration)
  6. formatDuration(long millis)
  7. formatDuration(long time)
  8. formatDurationSeconds(long seconds)
  9. formatDurationTill(long start)