Java Time Format formatTimeLength(long ms)

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

Description

format Time Length

License

Open Source License

Declaration

public static String formatTimeLength(long ms) 

Method Source Code

//package com.java2s;
/*/* ww  w  . j  a v a 2 s  .  c om*/
 * Copyright (C) ${year} Omry Yadan <${email}>
 * All rights reserved.
 *
 * See https://github.com/omry/banana/blob/master/BSD-LICENSE for licensing information
 */

public class Main {
    public static String formatTimeLength(long ms) {
        int SECOND = 1000;
        int MINUTE = SECOND * 60;
        int HOUR = MINUTE * 60;
        int DAY = HOUR * 24;
        if (ms < SECOND)
            return ms + " ms";
        if (ms < MINUTE)
            return round((ms / (float) SECOND), 2) + " secs";
        if (ms < HOUR)
            return round((ms / (float) MINUTE), 2) + " mins";
        if (ms < DAY)
            return round((ms / (float) HOUR), 2) + " hours";
        else
            return round((ms / (float) DAY), 2) + " days";

    }

    private static String round(float f, int n) {
        int d = (int) Math.pow(10, n);
        return "" + ((int) (f * d)) / (float) d;
    }
}

Related

  1. formatTimeInfo(long time)
  2. formatTimeInMilisec(long time)
  3. formatTimeInNanos(long time)
  4. formatTimeInterval(final long time)
  5. formatTimeInterval(long in)
  6. formatTimeLikeTimer(long time, boolean appendMs)
  7. formatTimeNicely(long millis)
  8. formatTimeOffset(long offset)
  9. formatTimePart2(long number, String unit)