Kyan
Kyan

Reputation: 505

Spring Form - using Date and Time as input type to be assigned to a LocalDate and LocalTime variable. typeMismatch issue

I have the following spring form

<form:form action="/management/recruitment/setInterview" modelAttribute="interview">
Date<form:input type="Date" path="interviewDate"/><br>
Time<form:input type="Time" path="interviewTime"/>
<br>
<input type="submit" value="Invite for interview" id="submitButton" />

P.S. I am using java.time.LocalDate and LocalTime and my both variables are annotated with @DateTimeFormat.

Upvotes: 3

Views: 2713

Answers (1)

dyslexit
dyslexit

Reputation: 773

Annotate your interviewDate and interviewTime fields in the interview POJO with DateTimeFormat, like so:

@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
private LocalDate interviewDate;

Note that if the submitting date or time String from the user isn't in DateTimeFormat.ISO format you can give it a custom pattern, such as @DateTimeFormat(pattern = "MM-dd-yyyy")

One other way to do this is to use an @InitBinder in the controller (example), but for something as simple as this DateTimeFormat should be enough.

Upvotes: 4

Related Questions