DateTimeUtils { public static String formatUptime(long startTime) { long seconds = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - startTime); final long days = seconds / TimeUnit.DAYS.toSeconds(1); seconds -= TimeUnit.DAYS.toSeconds(days); final long hours = seconds / TimeUnit.HOURS.toSeconds(1); seconds -= TimeUnit.HOURS.toSeconds(hours); final long minutes = seconds / TimeUnit.MINUTES.toSeconds(1); seconds -= TimeUnit.MINUTES.toSeconds(minutes); return String.format("%dd:%dh:%dm:%ds", days, hours, minutes, seconds); }  static String formatDateTime(long time); static String formatUptime(long startTime);  }
@Test public void formatUptimeTest() { long start = System.currentTimeMillis(); final String oneSecond = DateTimeUtils.formatUptime(start - TimeUnit.SECONDS.toMillis(1)); assertEquals(oneSecond, "0d:0h:0m:1s"); final String oneMinute = DateTimeUtils.formatUptime(start - TimeUnit.MINUTES.toMillis(1)); assertEquals(oneMinute, "0d:0h:1m:0s"); final String oneHour = DateTimeUtils.formatUptime(start - TimeUnit.HOURS.toMillis(1)); assertEquals(oneHour, "0d:1h:0m:0s"); final String oneDay = DateTimeUtils.formatUptime(start - TimeUnit.DAYS.toMillis(1)); assertEquals(oneDay, "1d:0h:0m:0s"); final String twoTwoTwoTwo = DateTimeUtils.formatUptime( start - TimeUnit.DAYS.toMillis(2) - TimeUnit.HOURS.toMillis(2) - TimeUnit.MINUTES.toMillis(2) - TimeUnit.SECONDS.toMillis(2)); assertEquals(twoTwoTwoTwo, "2d:2h:2m:2s"); }