如何使用 Spring MVC 将表单参数绑定到 Java 8 Duration?

Wim*_*uwe 6 java spring-mvc jsr310

我知道我可以使用以下方法将 Spring MVC 表单 bean 绑定到 LocalDateTime:

@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
private LocalDateTime startTime;
Run Code Online (Sandbox Code Playgroud)

这会将 String like2016-01-11T15:05:05.123Z转换为LocalDateTime对象。

我在这里找到了一些文档:http : //blog.codeleak.pl/2014/06/spring-4-datetimeformat-with-java-8.html

我找不到如何对java.time.Duration. 我该怎么做呢?

Wim*_*uwe 4

看来什么都不需要了。它开箱即用(使用 Spring Boot 1.3.1 和 Spring 4.2.4),具有 Duration 和 ZoneId:

private Duration duration;
private ZoneId timeZone;
Run Code Online (Sandbox Code Playgroud)

我使用“PT15H”字符串测试持续时间,使用“Europe/Brussels”测试时区。