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