DateToJodaTimeBaseLocalConverter extends AbstractConverter { @SuppressWarnings("unchecked") @Override public Object convert(Object source, TypeToken<?> targetTypeToken) { if (!canHandle(source, targetTypeToken)) { throw new ConverterException(source, targetTypeToken.getRawType()); } Constructor constructor; try { constructor = ((Class) targetTypeToken.getType()).getConstructor(Object.class); } catch (NoSuchMethodException e) { throw new ConverterException(source, targetTypeToken.getRawType(), e); } try { return constructor.newInstance(source); } catch (Exception e) { throw new ConverterException(source, targetTypeToken.getRawType(), e); } }  @SuppressWarnings("unchecked") @Override Object convert(Object source, TypeToken<?> targetTypeToken); @Override boolean canHandle(Object source, TypeToken<?> targetTypeToken);  }
@Test(expected = ConverterException.class) public void testConvertExceptionByDest() { converter.convert(new Date(), TypeToken.of(Duration.class)); }
@Test public void convert() throws Exception { String dateString = "06/27/2017 12:30"; DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm"); Date date = df.parse(dateString); LocalTime localTime = (LocalTime) converter.convert(date, TypeToken.of(LocalTime.class)); assertEquals(12, localTime.getHourOfDay()); assertEquals(30, localTime.getMinuteOfHour()); LocalDate localDate = (LocalDate) converter.convert(date, TypeToken.of(LocalDate.class)); assertEquals(2017, localDate.getYear()); assertEquals(6, localDate.getMonthOfYear()); assertEquals(27, localDate.getDayOfMonth()); LocalDateTime localDateTime = (LocalDateTime) converter.convert(date, TypeToken.of(LocalDateTime.class)); assertEquals(12, localDateTime.getHourOfDay()); assertEquals(30, localDateTime.getMinuteOfHour()); assertEquals(2017, localDateTime.getYear()); assertEquals(6, localDateTime.getMonthOfYear()); assertEquals(27, localDateTime.getDayOfMonth()); }
@Test(expected = ConverterException.class) public void testConvertExceptionBySource() { converter.convert("2017-01-01", TypeToken.of(LocalTime.class)); }