Java Duration Format formatDuration(long millis)

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

Description

Returns specified duration as a string in the following format: "hours:minutes:seconds.milliseconds".

License

Apache License

Declaration

public static String formatDuration(long millis) 

Method Source Code

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

public class Main {
    /**/*  w w w. jav  a  2 s . c o m*/
     * Returns specified duration as a string in the following format:
     * "hours:minutes:seconds.milliseconds".
     */
    public static String formatDuration(long millis) {
        final int millisInSecond = 1000;
        final int millisInMinute = 60 * 1000;
        final int millisInHour = 60 * 60 * 1000;

        int hours = (int) (millis / millisInHour);
        millis = millis - (hours * millisInHour);
        int minutes = (int) (millis / millisInMinute);
        millis = millis - (minutes * millisInMinute);
        int seconds = (int) (millis / millisInSecond);
        millis = millis - (seconds * millisInSecond);
        int milliseconds = (int) millis;

        StringBuilder result = new StringBuilder(16);
        result.append(hours);
        result.append(':');
        result.append((char) (minutes / 10 + '0'));
        result.append((char) (minutes % 10 + '0'));
        result.append(':');
        result.append((char) (seconds / 10 + '0'));
        result.append((char) (seconds % 10 + '0'));
        result.append('.');
        if (milliseconds < 10) {
            result.append('0').append('0');
        } else if (milliseconds < 100) {
            result.append('0');
        }
        result.append(milliseconds);

        return result.toString();
    }
}

Related

  1. formatDuration(long elapsed)
  2. formatDuration(long elapsedSec)
  3. formatDuration(Long input)
  4. formatDuration(long milis)
  5. formatDuration(long millis)
  6. formatDuration(long milliseconds)
  7. formatDuration(long milliseconds)
  8. formatDuration(long ms)
  9. formatDuration(long ms)