### Question:
DatePropertyConverter extends AbstractPropertyConverter { @Override public Object convert(String value, Context context) throws IllegalArgumentException { String pattern = context.resolve(PATTERN, ISO8601); DateFormat df = new SimpleDateFormat(pattern); try { Date date = df.parse(value); if (Date.class.isAssignableFrom(context.getTargetType())) { return date; } Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return calendar; } catch (ParseException ex) { throw new IllegalArgumentException(ex.getMessage(), ex); } }  @Override boolean supports(Class<?> type); @Override Object convert(String value, Context context); static final String PATTERN; static final String ISO8601; }### Answer:
@Test public void testConvertToDate() throws Exception { context.checking(newContextExpectations(Date.class, DatePropertyConverter.ISO8601)); assertThat((Date) converter.convert(nowString, converterContext), is(equalTo(now))); }
@Test public void testConvertToCalendar() throws Exception { context.checking(newContextExpectations(Calendar.class, DatePropertyConverter.ISO8601)); Calendar calendar = (Calendar) converter.convert(nowString, converterContext); assertThat(calendar.getTime(), is(equalTo(now))); }
@Test public void testCustomFormat() throws Exception { String pattern = "EEE, d MMM yyyy HH:mm:ss.SSS Z"; SimpleDateFormat df = new SimpleDateFormat(pattern); String nowString = df.format(now); context.checking(newContextExpectations(Date.class, pattern)); Date date = (Date) converter.convert(nowString, converterContext); assertThat(date, is(equalTo(now))); }