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