7

I know I can bind a Spring MVC form bean to a LocalDateTime using:

@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
private LocalDateTime startTime;

This will convert a String like 2016-01-11T15:05:05.123Z to a LocalDateTime object.

I have found some docs on this here: http://blog.codeleak.pl/2014/06/spring-4-datetimeformat-with-java-8.html

I could not find how to do the same with java.time.Duration. How do I do this?

Wim Deblauwe
  • 25,113
  • 20
  • 133
  • 211
  • `@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)` isn't mandatory – Alex78191 May 03 '17 at 00:34
  • Make sure you spelled the duration correctly - `PT1D` is invalid, should be `P1D`. Hours and smaller need `PT`. And you can combine with `P1DT1H`. – Radu Nov 14 '22 at 18:48

1 Answers1

4

It seems nothing is needed. It works out of the box (Using Spring Boot 1.3.1 with Spring 4.2.4) with Duration and ZoneId:

private Duration duration;
private ZoneId timeZone;

I tested with "PT15H" string for duration and "Europe/Brussels" for timeZone.

Wim Deblauwe
  • 25,113
  • 20
  • 133
  • 211