CompositeDateTimeParser implements DateTimeParser { @Override public Optional<Long> getEpochMilli(final String time) { for (final DateTimeParser parser : parsers) { try { return parser.getEpochMilli(time); } catch (Exception e) { LOGGER.debug("Could not parse time {} using parser {}", time, parser, e); } } return Optional.empty(); } CompositeDateTimeParser(final DateTimeParser... parsers);  CompositeDateTimeParser(final List<DateTimeParser> parsers); @Override Optional<Long> getEpochMilli(final String time);  }
@Test void shouldReturnFirstParsed() { final CompositeDateTimeParser parser = new CompositeDateTimeParser( new ZonedDateTimeParser(), new LocalDateTimeParser(ZoneOffset.UTC) ); final Optional<Long> parsed1 = parser.getEpochMilli("2017-10-05T10:36:22"); assertThat(parsed1) .hasValue(1507199782000L); final Optional<Long> parsed2 = parser.getEpochMilli("2018-05-31T14:05:25.155Z"); assertThat(parsed2) .hasValue(1527775525155L); }
@Test void shouldReturnEmptyOptionalIfNoMatchedFormat() { final CompositeDateTimeParser parser = new CompositeDateTimeParser( new ZonedDateTimeParser(), new LocalDateTimeParser(ZoneOffset.UTC) ); final Optional<Long> parsed = parser.getEpochMilli("2017-10-05T10:36:22UTC"); assertThat(parsed) .isEmpty(); }