DurationParser { public static long inMillis(String durationStr) throws NumberFormatException { String[] parts = durationStr.trim().split(":"); if (parts.length == 1) { return toMillis(parts[0]); } else if (parts.length == 2) { return toMillis("0", parts[0], parts[1]); } else if (parts.length == 3) { return toMillis(parts[0], parts[1], parts[2]); } else { throw new NumberFormatException(); } }  static long inMillis(String durationStr);  }
@Test public void testSecondDurationInMillis() { long duration = DurationParser.inMillis("00:45"); assertEquals(45 * seconds, duration); }
@Test public void testSingleNumberDurationInMillis() { int twoHoursInSeconds = 2 * 60 * 60; long duration = DurationParser.inMillis(String.valueOf(twoHoursInSeconds)); assertEquals(2 * hours, duration); }
@Test public void testMinuteSecondDurationInMillis() { long duration = DurationParser.inMillis("05:10"); assertEquals(5 * minutes + 10 * seconds, duration); }
@Test public void testHourMinuteSecondDurationInMillis() { long duration = DurationParser.inMillis("02:15:45"); assertEquals(2 * hours + 15 * minutes + 45 * seconds, duration); }
@Test public void testSecondsWithMillisecondsInMillis() { long duration = DurationParser.inMillis("00:00:00.123"); assertEquals(123, duration); }