DateTypeConverter implements TypeConverter<String, Date> { @Override public Date convert(final String value) { Utils.checkArgument(value != null, "Value to convert must not be null"); Utils.checkArgument(!value.isEmpty(), "Value to convert must not be empty"); try { return new SimpleDateFormat(dateFormat).parse(value); } catch (ParseException e) { throw new IllegalArgumentException("Unable to convert value '" + value + "' to a Date object with format " + dateFormat, e); } } DateTypeConverter();  DateTypeConverter(String dateFormat); @Override Date convert(final String value); static final String DEFAULT_DATE_FORMAT; }
@Test public void whenInputValueIsLegalValue_ThenShouldReturnValidDate() { String date = "2015-01-01"; Date convertedDate = converter.convert(date); assertThat(convertedDate).isNotNull(); Date expectedDate = java.sql.Date.valueOf(date); assertThat(convertedDate.getTime()).isEqualTo(expectedDate.getTime()); }
@Test(expected = IllegalArgumentException.class) public void whenValueIsIllegal_ThenShouldThrowAnIllegalArgumentException() { converter.convert("foo"); }