LocalDateTimeConverter extends AbstractJava8DateTimeConverter { @Override public Object convert(Class destClass, Object srcObject) { LocalDateTime localDateTime = convertToLocalDateTime(srcObject); if (localDateTime != null) { if (LocalTime.class.isAssignableFrom(destClass)) { return localDateTime.toLocalTime(); } else if (LocalDate.class.isAssignableFrom(destClass)) { return localDateTime.toLocalDate(); } return localDateTime; } return super.convert(destClass, srcObject); } LocalDateTimeConverter(DateTimeFormatter formatter); @Override Object convert(Class destClass, Object srcObject);  }
@Test public void canConvertFromLocalDateTimeToLocalDateTimeViaDateTime() { LocalDateTime sourceObject = LocalDateTime.of(2017, 11, 2, 10, 0); LocalDateTime expectedResult = LocalDateTime.of(2017, 11, 2, 10, 0); LocalDateTime result = (LocalDateTime)localDateTimeConverter.convert(LocalDateTime.class, sourceObject); assertTrue(String.format("%s is not equal to %s", result, expectedResult), expectedResult.isEqual(result)); }
@Test public void canConvertFromOffsetDateTimeToLocalDateTimeViaDateTime() { OffsetDateTime sourceObject = OffsetDateTime.parse("2017-11-02T10:20:30+02:00"); LocalDateTime expectedResult = OffsetDateTime.parse("2017-11-02T10:20:30+02:00").toLocalDateTime(); LocalDateTime result = (LocalDateTime)localDateTimeConverter.convert(LocalDateTime.class, sourceObject); assertTrue(String.format("%s is not equal to %s", result, expectedResult), expectedResult.isEqual(result)); }
@Test public void canConvertFromZonedDateTimeToLocalDateTimeViaDateTime() { ZonedDateTime sourceObject = ZonedDateTime.parse("2017-11-02T10:20:30+02:00[Europe/Paris]"); LocalDateTime expectedResult = ZonedDateTime.parse("2017-11-02T10:20:30+02:00[Europe/Paris]").toLocalDateTime(); LocalDateTime result = (LocalDateTime)localDateTimeConverter.convert(LocalDateTime.class, sourceObject); assertTrue(String.format("%s is not equal to %s", result, expectedResult), expectedResult.isEqual(result)); }
@Test public void canConvertFromStringToLocalDateTimeViaDateTime() { String sourceObject = "2017-11-02T10:20:30"; LocalDateTime expectedResult = LocalDateTime.parse("2017-11-02T10:20:30"); LocalDateTime result = (LocalDateTime)localDateTimeConverter.convert(LocalDateTime.class, sourceObject); assertTrue(String.format("%s is not equal to %s", result, expectedResult), expectedResult.isEqual(result)); }
@Test public void canConvertFromInstantToLocalDateTimeViaDateTime() { Instant sourceObject = Instant.parse("2017-11-02T10:20:30.00Z"); LocalDateTime expectedResult = LocalDateTime.ofInstant(Instant.parse("2017-11-02T10:20:30.00Z"), ZoneId.systemDefault()); LocalDateTime result = (LocalDateTime)localDateTimeConverter.convert(LocalDateTime.class, sourceObject); assertTrue(String.format("%s is not equal to %s", result, expectedResult), expectedResult.isEqual(result)); }
@Test public void canConvertFromLongToLocalDateTimeViaDateTime() { Long sourceObject = Instant.parse("2017-11-02T10:20:30.00Z").toEpochMilli(); LocalDateTime expectedResult = LocalDateTime.ofInstant(Instant.parse("2017-11-02T10:20:30.00Z"), ZoneId.systemDefault()); LocalDateTime result = (LocalDateTime)localDateTimeConverter.convert(LocalDateTime.class, sourceObject); assertTrue(String.format("%s is not equal to %s", result, expectedResult), expectedResult.isEqual(result)); }
@Test public void canConvertFromLocalDateToLocalDateTimeViaDate() { LocalDate sourceObject = LocalDate.of(2017, 11, 2); LocalDate expectedResult = LocalDate.of(2017, 11, 2); LocalDate result = (LocalDate)localDateConverter.convert(LocalDate.class, sourceObject); assertTrue(String.format("%s is not equal to %s", result, expectedResult), expectedResult.isEqual(result)); }
@Test public void canConvertFromOffsetDateTimeToLocalDateTimeViaDate() { OffsetDateTime sourceObject = OffsetDateTime.parse("2017-11-02T10:20:30+02:00"); LocalDate expectedResult = LocalDate.of(2017, 11, 2); LocalDate result = (LocalDate)localDateConverter.convert(LocalDate.class, sourceObject); assertTrue(String.format("%s is not equal to %s", result, expectedResult), expectedResult.isEqual(result)); }
@Test public void canConvertFromStringToLocalDateTimeViaDate() { String sourceObject = "2017-11-02"; LocalDate expectedResult = LocalDate.of(2017, 11, 2); LocalDate result = (LocalDate)localDateConverter.convert(LocalDate.class, sourceObject); assertTrue(String.format("%s is not equal to %s", result, expectedResult), expectedResult.isEqual(result)); }
@Test public void canConvertFromLongToLocalDateTimeViaDate() { Long sourceObject = Instant.parse("2017-11-02T10:20:30.00Z").toEpochMilli(); LocalDate expectedResult = LocalDate.of(2017, 11, 2); LocalDate result = (LocalDate)localDateConverter.convert(LocalDate.class, sourceObject); assertTrue(String.format("%s is not equal to %s", result, expectedResult), expectedResult.isEqual(result)); }
@Test public void canConvertFromDateToLocalDateTimeViaDate() { Date sourceObject = new Date(Instant.parse("2017-11-02T10:20:30.00Z").toEpochMilli()); LocalDate expectedResult = LocalDate.of(2017, 11, 2); LocalDate result = (LocalDate)localDateConverter.convert(LocalDate.class, sourceObject); assertTrue(String.format("%s is not equal to %s", result, expectedResult), expectedResult.isEqual(result)); }
@Test public void canConvertFromLocalTimeToLocalTimeViaTime() { LocalTime sourceObject = LocalTime.of(10, 20, 30); LocalTime expectedResult = LocalTime.of(10, 20, 30); LocalTime result = (LocalTime)localTimeConverter.convert(LocalTime.class, sourceObject); assertEquals(expectedResult, result); }
@Test public void canConvertFromOffsetDateTimeToLocalTimeViaTime() { OffsetDateTime sourceObject = OffsetDateTime.parse("2017-11-02T10:20:30+02:00"); LocalTime expectedResult = LocalTime.of(10, 20, 30); LocalTime result = (LocalTime)localTimeConverter.convert(LocalTime.class, sourceObject); assertEquals(expectedResult, result); }
@Test public void canConvertFromStringToLocalTimeViaTime() { String sourceObject = "10:20:30"; LocalTime expectedResult = LocalTime.of(10, 20, 30); LocalTime result = (LocalTime)localTimeConverter.convert(LocalTime.class, sourceObject); assertEquals(expectedResult, result); }
@Test public void canConvertFromLongToLocalTimeViaTime() { Long sourceObject = Instant.parse("2017-11-02T10:20:30.00Z").toEpochMilli(); LocalTime expectedResult = LocalDateTime.ofInstant(Instant.parse("2017-11-02T10:20:30.00Z"), ZoneId.systemDefault()).toLocalTime(); LocalTime result = (LocalTime)localTimeConverter.convert(LocalTime.class, sourceObject); assertEquals(expectedResult, result); }
@Test public void canConvertFromDateToLocalTimeViaTime() { Date sourceObject = new Date(Instant.parse("2017-11-02T10:20:30.00Z").toEpochMilli()); LocalTime expectedResult = LocalDateTime.ofInstant(Instant.parse("2017-11-02T10:20:30.00Z"), ZoneId.systemDefault()).toLocalTime(); LocalTime result = (LocalTime)localTimeConverter.convert(LocalTime.class, sourceObject); assertEquals(expectedResult, result); }
@Test(expected = ConversionException.class) public void testThrowExceptionWhenSourceNotSupported() { localTimeConverter.convert(LocalDateTime.class, 'a'); }