DurationConverter implements ITypeConverter<Duration> { @Override public Duration convert(String value) { try { String transformedText = formatter.toDurationString(value); return Duration.parse(transformedText); } catch(Exception e) { throw new RuntimeException(e); } } DurationConverter();  DurationConverter(IDurationFormatter formatter); @Override Duration convert(String value); @Override Type getTargetType(); @Override String toString();  }
@Test public void testConvert() { Duration expected = Duration.between(Instant.now().minus(100, ChronoUnit.SECONDS), Instant.now()); Duration val = new DurationConverter().convert(expected.toString()); Assert.assertEquals(expected, val); }
@Test public void testConvertCustomConverter() { String unformattedValue = "23:10"; DurationConverter customConverter = new DurationConverter(s -> { String[] parts = unformattedValue.split(":"); String hh = parts[0]; String mm = parts[1]; return String.format("PT%sH%sM", hh, mm); }); Duration expected = Duration.ofHours(23).plusMinutes(10); Duration actual = customConverter.convert(unformattedValue); Assert.assertEquals(expected, actual); }