String (long time) { if (time < 1000 * UNITS[0]) { return time + "ms"; } String result = ""; long remainder = time / 1000; long msec = time % 1000; for (int i = UNITS.length - 1; i >= 0; i--) { if (remainder < UNITS[i]) continue; long quotient = remainder / UNITS[i]; remainder = remainder % UNITS[i]; if (i == 0) { result += quotient + (msec == 0 ? "" : "." + String.format(M_SEC_FORMAT, msec)) + UNIT_NAMES[i]; } else { result += quotient + UNIT_NAMES[i]; if (remainder != 0 || msec != 0) { result += " "; if (remainder == 0) { result += "0." + String.format(M_SEC_FORMAT, msec) + UNIT_NAMES[0]; } } } } return result; }