Here you can find the source of prettyPrintMillis(long millis)
Parameter | Description |
---|---|
millis | time in millis |
public static String prettyPrintMillis(long millis)
//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?"; } } }