Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I bind a form parameter to a Java 8 Duration using Spring MVC?

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?

like image 917
Wim Deblauwe Avatar asked Jan 14 '16 20:01

Wim Deblauwe


People also ask

What is form binding in Spring MVC?

Form Handling Support in Spring MVC Model: basically a POJO (Plain Old Java Object) class is created to bind form fields with properties of the object. This object will be put into the model (model object).

What is spring bind tag?

General information. The spring:bind tag provides you with support for evaluation of the status of a certain bean or bean property.


1 Answers

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.

like image 181
Wim Deblauwe Avatar answered Oct 08 '22 11:10

Wim Deblauwe