Java Duration Format formatDuration(long ms)

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

Description

NOTE: formatDuration2() recommended in most cases for readability

License

Open Source License

Declaration

public static String formatDuration(long ms) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*from   w  w  w .  jav a 2s  .  c  om*/
     *  NOTE: formatDuration2() recommended in most cases for readability
     */
    public static String formatDuration(long ms) {
        if (ms < 5 * 1000) {
            return ms + "ms";
        } else if (ms < 3 * 60 * 1000) {
            return (ms / 1000) + "s";
        } else if (ms < 120 * 60 * 1000) {
            return (ms / (60 * 1000)) + "m";
        } else if (ms < 3 * 24 * 60 * 60 * 1000) {
            return (ms / (60 * 60 * 1000)) + "h";
        } else if (ms < 3L * 365 * 24 * 60 * 60 * 1000) {
            return (ms / (24 * 60 * 60 * 1000)) + "d";
        } else if (ms < 1000L * 365 * 24 * 60 * 60 * 1000) {
            return (ms / (365L * 24 * 60 * 60 * 1000)) + "y";
        } else {
            return "n/a";
        }
    }
}

Related

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