OffsetDateTimeConverter implements PropertyConverter<OffsetDateTime> { @Override public OffsetDateTime convert(String value, ConversionContext ctx) { ctx.addSupportedFormats(getClass(), OffsetDateTime.now().toString()); if(value==null){ return null; } try{ return OffsetDateTime.parse(value); }catch(Exception e){ LOG.log(Level.FINEST, e, () -> "Cannot parse OffsetDateTime: " + value); return null; } }  @Override OffsetDateTime convert(String value, ConversionContext ctx); @Override boolean equals(Object o); @Override int hashCode();  }
@Test public void convert() throws Exception { OffsetDateTimeConverter conv = new OffsetDateTimeConverter(); OffsetDateTime value = conv.convert("2007-12-03T10:15:30+01:00", context); assertThat(OffsetDateTime.parse("2007-12-03T10:15:30+01:00")).isEqualTo(value); value = conv.convert("foo", context); assertThat(value).isNull(); }
@Test public void callToConvertAddsMoreSupportedFormatsToTheContext() throws Exception { ConversionContext context = new ConversionContext.Builder(TypeLiteral.of(OffsetDateTime.class)).build(); OffsetDateTimeConverter converter = new OffsetDateTimeConverter(); converter.convert("", context); assertThat(context.getSupportedFormats().toString()).contains(" (OffsetDateTimeConverter)"); }