Java Duration Format formatDuration(long ms)

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

Description

format Duration

License

Open Source License

Declaration

public static String formatDuration(long ms) 

Method Source Code

//package com.java2s;
// it under the terms of the GNU General Public License as published by

public class Main {
    public static String formatDuration(long ms) {
        double nbSec = (double) ms / 1000.0;
        return formatDuration(nbSec);
    }/*w  ww  .ja v a  2s  .c  o  m*/

    public static String formatDuration(double nbSeconds) {
        if (nbSeconds < 10.0) {
            String str = "" + nbSeconds;
            int idx = str.indexOf('.');
            if (idx > 0)
                if (str.length() > idx + 3)
                    str = str.substring(0, idx + 3);
            return str + " sec.";
        } else {
            int intDuration = (int) nbSeconds;
            if (nbSeconds < 60.0)
                return "" + intDuration + " sec.";
            else if (intDuration < 3600) {
                int mn = intDuration / 60;
                int sec = intDuration - 60 * mn;
                return "" + mn + " mn " + ((sec < 10) ? "0" : "") + sec + " sec.";
            } else if (intDuration < 24 * 3600) {
                int h = intDuration / 3600;
                intDuration -= h * 3600;
                int mn = intDuration / 60;
                int sec = intDuration - 60 * mn;
                return "" + h + " h " + ((mn < 10) ? "0" : "") + mn + " mn " + ((sec < 10) ? "0" : "") + sec
                        + " sec.";
            } else {
                int days = intDuration / (24 * 3600);
                return "" + days + " day" + ((days > 1) ? "s " : " ")
                        + formatDuration((double) intDuration - (days * 24 * 3600));
            }
        }
    }
}

Related

  1. formatDuration(long milis)
  2. formatDuration(long millis)
  3. formatDuration(long millis)
  4. formatDuration(long milliseconds)
  5. formatDuration(long milliseconds)
  6. formatDuration(long ms)
  7. formatDuration(long number)
  8. formatDuration(long seconds)
  9. formatDuration(long seconds)