Java Duration Format formatDuration(long duration)

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

Description

Formats duration in miliseconds to hh:mm:ss string.

License

LGPL

Parameter

Parameter Description
duration a parameter

Return

Formatted duration.

Declaration

public static String formatDuration(long duration) 

Method Source Code

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

public class Main {
    /**/*from  w w w. ja v  a  2 s  .c  om*/
     * Formats duration in miliseconds to hh:mm:ss string. Returns empty string
     * for duration lesser than zero.
     * 
     * @param duration
     * @return Formatted duration.
     */
    public static String formatDuration(long duration) {
        if (duration < 0) {
            return "";
        }
        //to seconds
        duration /= 1000;
        short seconds = (short) (duration % 60);
        duration -= seconds;
        //to minutes
        duration /= 60;
        short minutes = (short) (duration % 60);
        duration -= minutes;
        short hours = (short) (duration / 60);

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

Related

  1. formatDuration(final long milliSeconds)
  2. formatDuration(final long seconds)
  3. formatDuration(int durationMins)
  4. formatDuration(int seconds)
  5. formatDuration(int seconds)
  6. formatDuration(long duration)
  7. formatDuration(long duration)
  8. formatDuration(long duration)
  9. formatDuration(long duration, boolean displayMilliseconds)