DateFormatterProvider { public Date toDate(Object value) { if (value instanceof String) { return parse((String) value); } if (value instanceof Date) { return (Date)value; } if (value instanceof Long) { return new Date((long)value); } throw new DateTimeException(MessageFormat.format("Error while parsing date. Type: {0}, value: {1}", value.getClass().getName(), value)); } DateFormatterProvider(String dateFormatPattern); ZoneId getZoneId(); String getDateFormatPattern(); void setDateFormatPattern(String dateFormatPattern); Date parse(String value); Date toDate(Object value);  }
@Test public void should_returnDate_when_stringRepresentsADate() { String dateStr = "1970-01-01"; Date date = provider.toDate(dateStr); assertThat(date).hasTime(0); }
@Test public void should_returnDate_when_stringRepresentsADateWithTimeInformation() { String dateStr = "1970-01-01T01:01:01.001Z"; long time = Duration.ofHours(1).toMillis() + Duration.ofMinutes(1).toMillis() + Duration.ofSeconds(1).toMillis() + 1; Date date = provider.toDate(dateStr); assertThat(date).hasTime(time); }
@Test public void should_returnDate_when_longIsProvided() { long time = 1000; Date date = provider.toDate(time); assertThat(date).hasTime(time); }
@Test public void should_returnDate_when_dateIsProvided() { Date initialDate = new Date(1000); Date date = provider.toDate(initialDate); assertThat(date).isEqualTo(initialDate); }
@Test public void should_throwException_when_isNotAStringADateOrALong() { double value = 1.2; assertThatExceptionOfType(DateTimeException.class) .isThrownBy( () -> provider.toDate(value)) .withMessageContaining("Error while parsing date"); }