Java Millisecond Format prettyPrintMillis(long millis)

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

Description

Prints a time for display

License

Apache License

Parameter

Parameter Description
millis time in millis

Return

the time, represented as millis, seconds, minutes or hours as appropriate, with suffix

Declaration

public static String prettyPrintMillis(long millis) 

Method Source Code

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

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class Main {
    private static final NumberFormat MEM_FMT = new DecimalFormat("##,###.##");

    /**//from  ww  w . j  a va 2  s.  co m
     * Prints a time for display
     *
     * @param millis time in millis
     * @return the time, represented as millis, seconds, minutes or hours as appropriate, with suffix
     */
    public static String prettyPrintMillis(long millis) {
        if (millis < 1000)
            return millis + " milliseconds";
        NumberFormat nf = NumberFormat.getNumberInstance();
        nf.setMaximumFractionDigits(2);
        double toPrint = ((double) millis) / 1000;
        if (toPrint < 300) {
            return nf.format(toPrint) + " seconds";
        }

        toPrint = toPrint / 60;

        if (toPrint < 120) {
            return nf.format(toPrint) + " minutes";
        }

        toPrint = toPrint / 60;

        return nf.format(toPrint) + " hours";
    }

    private static String format(long bytes) {
        double val = bytes;
        int mag = 0;
        while (val > 1024) {
            val = val / 1024;
            mag++;
        }

        String formatted = MEM_FMT.format(val);
        switch (mag) {
        case 0:
            return formatted + " bytes";
        case 1:
            return formatted + " kb";
        case 2:
            return formatted + " Mb";
        case 3:
            return formatted + " Gb";
        default:
            return "WTF?";
        }
    }
}

Related

  1. millisPerBeat(final float bpm)
  2. millisSince(long from)
  3. millisToDate(long millis, String format)
  4. millisTrimTo(long millis, long unitInMillis)
  5. parseDate(long millisec, String format)
  6. prettyPrintMillis(long millis)
  7. prettyPrintTime(long millis)
  8. printDate(long millis)
  9. reverseFormattedDateWithMilliseconds()