OffsetDateTimeConverter extends AbstractJava8DateTimeConverter { @Override public Object convert(Class destClass, Object srcObject) { OffsetDateTime offsetDateTime = convertToOffsetDateTime(srcObject); if (offsetDateTime != null) { if (OffsetTime.class.isAssignableFrom(destClass)) { return offsetDateTime.toOffsetTime(); } return offsetDateTime; } return super.convert(destClass, srcObject); } OffsetDateTimeConverter(DateTimeFormatter formatter); @Override Object convert(Class destClass, Object srcObject);  }
@Test public void canConvertFromOffsetDateTimeToOffsetDateTimeViaDate() { OffsetDateTime sourceObject = OffsetDateTime.of(2017, 11, 2, 10, 20, 30, 0, ZoneOffset.UTC); OffsetDateTime expectedResult = OffsetDateTime.of(2017, 11, 2, 10, 20, 30, 0, ZoneOffset.UTC); OffsetDateTime result = (OffsetDateTime)offsetDateTimeConverterDate.convert(OffsetDateTime.class, sourceObject); assertTrue(expectedResult.isEqual(result)); }
@Test public void canConvertFromOffsetDateTimeStringToOffsetDateTime() { OffsetDateTime sourceObject = OffsetDateTime.parse("2017-11-02T10:20:30+02:00"); OffsetDateTime expectedResult = OffsetDateTime.of(2017, 11, 2, 10, 20, 30, 0, ZoneOffset.ofHours(2)); OffsetDateTime result = (OffsetDateTime)offsetDateTimeConverterDate.convert(OffsetDateTime.class, sourceObject); assertTrue(expectedResult.isEqual(result)); }
@Test public void canConvertFromZonedDateTimeToOffsetDateTimeViaDate() { ZonedDateTime sourceObject = ZonedDateTime.parse("2017-11-02T10:20:30+01:00[Europe/Paris]"); OffsetDateTime expectedResult = OffsetDateTime.of(2017, 11, 2, 10, 20, 30, 0, ZoneOffset.ofHours(1)); OffsetDateTime result = (OffsetDateTime)offsetDateTimeConverterDate.convert(OffsetDateTime.class, sourceObject); assertTrue(expectedResult.isEqual(result)); }
@Test public void canConvertFromStringToOffsetDateTimeViaDate() { String sourceObject = "2017-11-02T10:20:30+02:00"; OffsetDateTime expectedResult = OffsetDateTime.of(2017, 11, 2, 10, 20, 30, 0, ZoneOffset.ofHours(2)); OffsetDateTime result = (OffsetDateTime)offsetDateTimeConverterDate.convert(OffsetDateTime.class, sourceObject); assertTrue(expectedResult.isEqual(result)); }
@Test public void canConvertFromInstantToOffsetDateTimeViaDate() { Instant sourceObject = Instant.parse("2017-11-02T10:20:30.00Z"); OffsetDateTime expectedResult = OffsetDateTime.ofInstant(Instant.parse("2017-11-02T10:20:30.00Z"), ZoneId.systemDefault()); OffsetDateTime result = (OffsetDateTime)offsetDateTimeConverterDate.convert(OffsetDateTime.class, sourceObject); assertTrue(expectedResult.isEqual(result)); }
@Test public void canConvertFromLongToOffsetDateTimeViaDate() { Long sourceObject = Instant.parse("2017-11-02T10:20:30.00Z").toEpochMilli(); OffsetDateTime expectedResult = OffsetDateTime.ofInstant(Instant.parse("2017-11-02T10:20:30.00Z"), ZoneId.systemDefault()); OffsetDateTime result = (OffsetDateTime)offsetDateTimeConverterDate.convert(OffsetDateTime.class, sourceObject); assertTrue(expectedResult.isEqual(result)); }
@Test public void canConvertFromDateToOffsetDateTimeViaDate() { Date sourceObject = new Date(Instant.parse("2017-11-02T10:20:30.00Z").toEpochMilli()); OffsetDateTime expectedResult = OffsetDateTime.ofInstant(Instant.parse("2017-11-02T10:20:30.00Z"), ZoneId.systemDefault()); OffsetDateTime result = (OffsetDateTime)offsetDateTimeConverterDate.convert(OffsetDateTime.class, sourceObject); assertTrue(expectedResult.isEqual(result)); }
@Test public void canConvertFromOffsetTimeToOffsetDateTimeViaTime() { OffsetTime sourceObject = OffsetTime.of(LocalTime.of(10, 20, 30), ZoneOffset.ofHours(2)); OffsetTime expectedResult = OffsetTime.of(LocalTime.of(10, 20, 30), ZoneOffset.ofHours(2)); OffsetTime result = (OffsetTime)offsetDateTimeConverterTime.convert(OffsetTime.class, sourceObject); assertTrue(expectedResult.isEqual(result)); }
@Test public void canConvertFromStringToOffsetDateTimeViaTime() { String sourceObject = "10:20:30+04:00"; OffsetTime expectedResult = OffsetTime.of(LocalTime.of(10, 20, 30), ZoneOffset.ofHours(4)); OffsetTime result = (OffsetTime)offsetDateTimeConverterTime.convert(OffsetTime.class, sourceObject); assertTrue(expectedResult.isEqual(result)); }
@Test public void canConvertFromLongToOffsetDateTimeViaTime() { Long sourceObject = Instant.parse("2017-11-02T10:20:30.00Z").toEpochMilli(); OffsetTime expectedResult = OffsetDateTime.ofInstant(Instant.parse("2017-11-02T10:20:30.00Z"), ZoneId.systemDefault()).toOffsetTime(); OffsetTime result = (OffsetTime)offsetDateTimeConverterTime.convert(OffsetTime.class, sourceObject); assertTrue(expectedResult.isEqual(result)); }
@Test public void canConvertFromDateToOffsetDateTimeViaTime() { Date sourceObject = new Date(Instant.parse("2017-11-02T10:20:30.00Z").toEpochMilli()); OffsetTime expectedResult = OffsetDateTime.ofInstant(Instant.parse("2017-11-02T10:20:30.00Z"), ZoneId.systemDefault()).toOffsetTime(); OffsetTime result = (OffsetTime)offsetDateTimeConverterTime.convert(OffsetTime.class, sourceObject); assertTrue(expectedResult.isEqual(result)); }