Java Millisecond Format prettyPrintTime(long millis)

Here you can find the source of prettyPrintTime(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 prettyPrintTime(long millis) 

Method Source Code

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

import java.text.NumberFormat;

import java.util.concurrent.TimeUnit;

public class Main {
    public static String prettyPrintTime(long time, TimeUnit unit) {
        return prettyPrintTime(unit.toMillis(time));
    }//from   w ww. j a  v  a 2 s .c o 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 prettyPrintTime(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";
    }
}

Related

  1. millisToDate(long millis, String format)
  2. millisTrimTo(long millis, long unitInMillis)
  3. parseDate(long millisec, String format)
  4. prettyPrintMillis(long millis)
  5. prettyPrintMillis(long millis)
  6. printDate(long millis)
  7. reverseFormattedDateWithMilliseconds()
  8. stringToTimestampInMilliseconds(String s, DateFormat formatter)
  9. strToDateMillisTimestamp(String dateStr, String formatStr)