UnitConverter { public static String toTimeUnit(long millis, int scale) { if (millis < MILLIS_PER_SECOND) { return String.format("%4dms", millis); } if (millis < MILLIS_PER_MINUTE) { return String.format("%" + (scale == 0 ? 2 : 3 + scale) + '.' + scale + "fs", millis * 1d / MILLIS_PER_SECOND); } if (millis < MILLIS_PER_HOUR) { return String.format("%" + (scale == 0 ? 2 : 3 + scale) + '.' + scale + "fm", millis * 1d / MILLIS_PER_MINUTE); } if (millis < MILLIS_PER_DAY) { return String.format("%" + (scale == 0 ? 2 : 3 + scale) + '.' + scale + "fh", millis * 1d / MILLIS_PER_HOUR); } return String.format("%" + (scale == 0 ? 2 : 3 + scale) + '.' + scale + "fd", millis * 1d / MILLIS_PER_DAY); }  static long toDurationMillis(String duration); static long toBytes(String size); static String toSizeUnit(Long bytes, int scale); static String toTimeUnit(long millis, int scale); static String toTimeWithMinorUnit(long millis);  }
@Test public void convertToTimeUnit() { assertThat(UnitConverter.toTimeUnit(1322L, 0)).isEqualTo(" 1s"); assertThat(UnitConverter.toTimeUnit(1322L, 1)).isEqualTo(" 1.3s"); assertThat(UnitConverter.toTimeUnit(1000L * 62, 0)).isEqualTo(" 1m"); assertThat(UnitConverter.toTimeUnit(1000L * 90, 0)).isEqualTo(" 2m"); assertThat(UnitConverter.toTimeUnit(1000L * 90, 1)).isEqualTo(" 1.5m"); assertThat(UnitConverter.toTimeUnit(1000L * 60 * 70, 1)).isEqualTo(" 1.2h"); assertThat(UnitConverter.toTimeUnit(1000L * 60 * 60 * 28, 1)).isEqualTo(" 1.2d"); }