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; import java.util.concurrent.TimeUnit; public class Main { private static final NumberFormat MEM_FMT = new DecimalFormat("##,###.##"); /**/*w w w . ja va2 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 prettyPrintMillis(long millis) { return prettyPrintTime(millis, TimeUnit.MILLISECONDS); } public static String prettyPrintTime(long time, TimeUnit unit) { NumberFormat nf = NumberFormat.getNumberInstance(); nf.setMaximumFractionDigits(2); if (unit.toNanos(time) < 1000) return nf.format(convert(time, unit, TimeUnit.NANOSECONDS)) + " ns"; if (unit.toMicros(time) < 1000) return nf.format(convert(time, unit, TimeUnit.MICROSECONDS)) + " us"; if (unit.toMillis(time) < 1000) return nf.format(convert(time, unit, TimeUnit.MILLISECONDS)) + " ms"; if (unit.toSeconds(time) < 300) return nf.format(convert(time, unit, TimeUnit.SECONDS)) + " seconds"; if (unit.toMinutes(time) < 120) return nf.format(convert(time, unit, TimeUnit.MINUTES)) + " minutes"; return nf.format(convert(time, unit, TimeUnit.HOURS)) + " 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?"; } } private static double convert(long time, TimeUnit unitIn, TimeUnit unitOut) { return (double) unitIn.toNanos(time) / (double) unitOut.toNanos(1); } }