TimeUtil { public static int parseDuration(String duration) { if (duration == null) { return 60 * 60 * 24 * 30; } int toAdd = -1; if (days.matcher(duration).matches()) { Matcher matcher = days.matcher(duration); matcher.matches(); toAdd = Integer.parseInt(matcher.group(1)) * (60 * 60) * 24; } else if (hours.matcher(duration).matches()) { Matcher matcher = hours.matcher(duration); matcher.matches(); toAdd = Integer.parseInt(matcher.group(1)) * (60 * 60); } else if (minutes.matcher(duration).matches()) { Matcher matcher = minutes.matcher(duration); matcher.matches(); toAdd = Integer.parseInt(matcher.group(1)) * (60); } else if (seconds.matcher(duration).matches()) { Matcher matcher = seconds.matcher(duration); matcher.matches(); toAdd = Integer.parseInt(matcher.group(1)); } if (toAdd == -1) { throw new IllegalArgumentException("Invalid duration pattern : " + duration); } return toAdd; }  static int parseDuration(String duration);  }
@Test public void test() { assertEquals(86400, TimeUtil.parseDuration("1d")); assertEquals(10, TimeUtil.parseDuration("10s")); assertEquals(2592000, TimeUtil.parseDuration("30d")); assertEquals(2592000, TimeUtil.parseDuration(null)); boolean catchedException = false; try { TimeUtil.parseDuration("NOT_A_VALID_INPUT"); } catch (IllegalArgumentException e) { catchedException = true; } assertTrue(catchedException); }