Here you can find the source of formatTime(long millis, int precision)
public static String formatTime(long millis, int precision)
//package com.java2s; public class Main { private static final String[] timeFormat = new String[] { "d ", "h ", "m ", "s ", "ms" }; public static String formatTime(long millis, int precision) { long[] la = new long[5]; la[0] = (millis / 86400000);// days la[1] = (millis / 3600000) % 24;// hours la[2] = (millis / 60000) % 60;// minutes la[3] = (millis / 1000) % 60;// seconds la[4] = (millis % 1000);// ms int index = 0; for (int i = 0; i < la.length; i++) { if (la[i] != 0) { index = i;/*from w w w. ja v a2s. c o m*/ break; } } StringBuilder buf = new StringBuilder(); int validLength = la.length - index; for (int i = 0; (i < validLength && i < precision); i++) { buf.append(la[index]).append(timeFormat[index]); index++; } return buf.toString(); } }