Java – Failed to convert property value of type ‘java.lang.String’ to required type ‘java.util.Date’


I am trying to implement the full calendar plugin in my webapp using Spring MVC, Java and MySql. I have been getting this error when I try to add a date using "input type = date" in my jsp:

Field error in object 'event' on field 'endDate': rejected value [2018-03-13]; 
codes [typeMismatch.event.endDate,typeMismatch.endDate,,typeMismatch];
arguments [ codes [event.endDate,endDate]; arguments [];
default message [endDate]]; default message [Failed to convert property value of type 'java.lang.String' 
to required type 'java.util.Date' for property 'endDate'; nested exception is org.springframework.core.convert.ConversionFailedException: 
Failed to convert from type java.lang.String to type @org.springframework.format.annotation.DateTimeFormat
java.util.Date for value '2018-03-13'; 
nested exception is java.lang.IllegalArgumentException: Unable to parse '2018-03-13']

In my controller class I am using SimpleDateFormat to format my date:

@RequestMapping(value = "add", method = RequestMethod.POST)
    public String add(@ModelAttribute("event") Event event,
            HttpServletRequest request,ServletRequestDataBinder binder,
            ModelMap modelMap){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("dd/MM/yyyy");

            return "redirect:../event.html";
        }catch (Exception e){
            modelMap.put("event", event);
            return "event/index";

And finally in my Jsp:

        <legend>Event Information</legend>
    <s:form method ="post" commandName = "event"
        action="${pageContext.request.contextPath }/event/add.html">
                <td><s:input path = "name"/></td>
                <td valign = "top">Description</td>
                <td><s:textarea path = "description" cols = "20" rows = "5" /></td>
                <td>Start Date</td>
                <td><input type = "date" name = "startDate" /></td>
                <td>End Date</td>
                <td><input type = "date" name = "endDate" /></td>
                <td><input type = "submit" value = "Save" /></td>

Here is the DAO Implementation:

public class EventDAOImpl implements EventDAO{

    private SessionFactory sessionFactory;

    public List<EventEntity> findAll() {
        List <EventEntity> list = null;
        Session session = null;
        Transaction transaction = null;
            session = sessionFactory.openSession();
            transaction = session .beginTransaction();
            list = session.createQuery("select as id, "
                    + " as title, "
                    + "DATE_FORMAT(e.startDate, '%Y-%m-%d') as start, "
                    + "DATE_FORMAT(e.endDate, '%Y-%m-%d') as end "
                    + "from Event e")
        }catch(Exception e){
            list = null;
            if(transaction != null){
        return list;

In my Entity class I have variables saved as a date so it is

private Date endDate;

I think the issue is with parsing of the Date but I am not sure! Any explanation of the issue would be appreciated.

Best Solution

@ModelAttribute("event") Event event will make Spring try to bind the request value 2018-03-13 to the private Date endDate field inside Event type. Your conversion code won't be invoked because the error happens before the add method is invoked.

You need to either define a global conversion logic using PropertyEditor or Converter as described here or use org.springframework.format.annotation.DateTimeFormat to specify the format for each date field:

@DateTimeFormat(pattern = "yyyy-MM-dd")
// or use @DateTimeFormat(pattern = DateTimeFormat.ISO.DATE)
private Date endDate;