DateFormatterProvider { public Date parse(String value) throws DateTimeException { DateTimeFormatter dateTimeFormatter = new DateTimeFormatterBuilder() .appendPattern(getDateFormatPattern()) .toFormatter() .withZone(getZoneId()); try { LocalDateTime localDateTime = dateTimeFormatter.parse(value, LocalDateTime::from); return Date.from(localDateTime.atZone(getZoneId()).toInstant()); } catch (DateTimeException e) { LocalDate localDate = dateTimeFormatter.parse(String.valueOf(value), LocalDate::from); return Date.from(localDate.atStartOfDay().atZone(getZoneId()).toInstant()); } } DateFormatterProvider(String dateFormatPattern); ZoneId getZoneId(); String getDateFormatPattern(); void setDateFormatPattern(String dateFormatPattern); Date parse(String value); Date toDate(Object value);  }
@Test public void should_throwException_when_stringIsNotADate() { String dateStr = "this is not a date"; assertThatExceptionOfType(DateTimeParseException.class) .isThrownBy(() -> provider.parse(dateStr)); }