ParameterUtil { public static Instant parseAlignedInstant(String dateHour, Schedule schedule) { final Schedule.WellKnown wellKnown = schedule.wellKnown(); Matcher matcher; final ZonedDateTime parsed; if (wellKnown == YEARLY && (matcher = YEAR_PATTERN.matcher(dateHour)).matches()) { parsed = LocalDate.of(parseInt(matcher.group(1)), 1, 1).atStartOfDay(UTC); } else if (wellKnown == MONTHLY && (matcher = YEAR_MONTH_PATTERN.matcher(dateHour)).matches()) { parsed = LocalDate.of(parseInt(matcher.group(1)), parseInt(matcher.group(2)), 1).atStartOfDay(UTC); } else { parsed = tryParseDateHour(dateHour); } final Instant instant = parsed.toInstant(); if (!TimeUtil.isAligned(instant, schedule)) { return TimeUtil.previousInstant(instant, schedule); } else { return instant; } } private  ParameterUtil(); static Instant parseDate(String date); static Instant parseDateHour(String dateHour); static Instant parseDateMonth(String dateMonth); static Instant parseDateYear(String dateYear); static String toParameter(Schedule schedule, Instant instant); static Timestamp parseBest(String value); static Instant parseAlignedInstant(String dateHour, Schedule schedule);  }
@Test public void parseAllExamples() { for (ParseExample example : PARSE_EXAMPLES) { Instant parsed = parseAlignedInstant(example.toParse(), example.schedule()); assertThat( "Parsing " + example.toParse() + " " + example.schedule(), parsed, is(example.expected())); } }
@Test public void unparsableExamples() { for (ParseExample example : UNPARSABLE) { try { parseAlignedInstant(example.toParse(), example.schedule()); fail(); } catch (IllegalArgumentException e) { continue; } fail(); } }