Failed to convert property value of type java.lang.String to required type java.util.Date

Today while adding to Java file a text box of date, i got below error while updating it:

Failed to convert property value of type java.lang.String to required type java.util.Date

Problem:
The problem here that Spring cannot put the data into object and cannot map it's format, the solution is to bind it to format you want to use.

Solution:
A data binder is needed, what is that ? how it's implemented ? let me answer those questions in the coming few lines.

If you have a date in a format like "mm-dd-yyyy" then it needs to be parsed, either you will do it manually after the @RequestParam Date or you will use like below or even using the WebDataBinder like Ex#2:

Ex#1
public void webmethod(@RequestParam("date") Date date) {
 }
Ex#2
public void initBinder(WebDataBinder binder){
     binder.registerCustomEditor(       Date.class,     
                         new CustomDateEditor(new SimpleDateFormat("dd/MM/yyyy"), true, 10));   
}

Where date is predefined earlier and automatically binds  using  Spring and @InitBinder annotation.

@InitBinder
public void dataBinding(WebDataBinder binder) {
	binder.addValidators(userValidator, emailValidator);
        //Below we are setting the Date format
	SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
	}

Now the error will be cleared as date will be understood.

How to format the date in the JSP ?
You need to add in the jsp a tag library which used for formatting which called JSTL format, below is the JSP "View" part:

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<fmt:formatDate pattern="dd-MM-yyyy" value="${project.projectStartdate}" />


java_jsp_date_picker

Date Picker
Below steps will are for adding the Date Picker for Java using the Bootstrap and Maven, first add below dependency to the pom.xml

<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap-datepicker</artifactId>
<version>1.0.1</version>
</dependency>

Save or ctrl+s then Maven will download the webjar automatically, you need to restart the webserver either it's an embeded Tomcat or something else so the new dependency gets loaded.

The Date Picker is a JavaScript so you need to add the JS to the JSP, to find the Date Picker path, go to left side panel under Maven dependencies and look for the bootstrap datepicker jar, under META-INF\resources\webjars\bootstrap-datepicker....etc "check below Fig#1" , then add the exact path starting from webjars inside the jsp "check Fig#2"

Fig#1
Springdepdency

 

 

 
Fig#2

http://webjars/bootstrap-datepicker/1.0.1/js/bootstrap-datepicker.js/script

Next step is to add the date picker to the input box, first chekc what is the input box name is "Ex. if it's targetDate", then you need to add at the end of the JSP the below script

$('#targetDate').datepicker({
formas : 'dd/mm/yyyy'
});