Java Millisecond Format formatMilliseconds (final long ms)

Here you can find the source of formatMilliseconds (final long ms)

Description

format Milliseconds

License

Apache License

Declaration

public static String formatMilliseconds (final long ms)
    

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file

public class Main {
    public static String formatMilliseconds(final long ms) {
        return formatNanoseconds(ms * 1_000_000L);
    }/*from   w ww.  ja  va  2 s . c om*/

    public static String formatNanoseconds(final long ns) {
        String s;

        final long NS_ONE_MICROSECOND = 1_000L;
        final long NS_ONE_MILLISECOND = 1_000L * NS_ONE_MICROSECOND;
        final long NS_ONE_SECOND = 1_000 * NS_ONE_MILLISECOND;
        final long NS_ONE_MINUTE = 60L * NS_ONE_SECOND;
        final long NS_ONE_HOUR = 60L * NS_ONE_MINUTE;
        final long NS_ONE_DAY = 24L * NS_ONE_HOUR;

        if (ns < NS_ONE_MICROSECOND) {
            s = String.format("%dns", ns);
        } else if (ns < NS_ONE_MILLISECOND) {
            s = String.format("%dus", ns / NS_ONE_MICROSECOND);
        } else if (ns < NS_ONE_SECOND) {
            s = String.format("%dms", ns / NS_ONE_MILLISECOND);
        } else if (ns < NS_ONE_MINUTE) {
            s = String
                    .format("%.1fs", (double) ns / (double) NS_ONE_SECOND);
        } else if (ns < NS_ONE_HOUR) {
            s = String
                    .format("%.1fm", (double) ns / (double) NS_ONE_MINUTE);
        } else if (ns < NS_ONE_DAY) {
            s = String.format("%.1fh", (double) ns / (double) NS_ONE_HOUR);
        } else {
            s = String.format("%.1fd", (double) ns / (double) NS_ONE_DAY);
        }

        return s;
    }
}

Related

  1. formatMillis(long time, boolean showMillis)
  2. formatMillis(long timeMillis)
  3. formatMillis(String millisecond, String format)
  4. formatMillisAsNumber(long millis)
  5. formatMillisAsShortHumanReadablePeriod(long millis)
  6. formatMilliseconds(Double milliseconds)
  7. formatMilliseconds(long milliseconds)
  8. formatMilliseconds(long milliseconds)
  9. formatMilliSecondsHumanReadable(long x)