DateParseUtil { public static TemporalAccessor dateParse(String value, Class<? extends TemporalAccessor> date, String...formats) { try { if (formats != null && formats.length > 0) { for (String form : formats) { try { try { return getParse(date, getFormat(form), value); } catch (DateTimeParseException e) { return getParse(date, value); } } catch (Exception e) { continue; } } } else { return getParse(date, value); } } catch (Exception e) { throw new RuntimeException(e); } catch (Throwable throwable) { throwable.printStackTrace(); } throw new RuntimeException("Can't format the date with the pattern"); }  static TemporalAccessor dateParse(String value, Class<? extends TemporalAccessor> date, String...formats);  }
@Test public void dateParseTest() { assertEquals(LocalDate.of(2018,01,01),dateParse("2018/01/01", LocalDate.class, parseList)); assertEquals(ZonedDateTime.of(2011,01,01,12,0,0,53810000,ZoneOffset.of("+01:00")),dateParse("2011-01-01T12:00:00.05381+01:00", ZonedDateTime.class, parseList)); assertEquals(LocalDateTime.of(2018,05,10,12,10),dateParse("2018-05-10 12:10", LocalDateTime.class, parseList)); assertEquals(LocalTime.of(12,10),dateParse("12 10", LocalTime.class, parseList)); assertEquals(OffsetTime.of(10,15,30,0,ZoneOffset.of("+01:00")),dateParse("10:15:30+01:00", OffsetTime.class, parseList)); }
@Test(expected = RuntimeException.class) public void dateParseErrorTest() throws Exception { try { dateParse("10/01/2010", LocalDateTime.class, parseList); } catch (Exception e) { assertTrue(e instanceof RuntimeException); assertEquals("Can't format the date with the pattern", e.getMessage()); throw e; } }