简体   繁体   中英

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

I need to save Date type in this Format(dd/mm/yyyy) and currently this format works for me(mm/dd/yyyy)

My jsp Code

<form:input path="fromDate" id="fromDate" />
<form:errors path="fromDate"/>

I am using a script for date format as "format: 'dd/mm/yyyy'"

Controller

public ModelAndView saveOrUpdateAcademic(Academic academic,final BindingResult errors) {
String string = request.getParameter("fromDate");
System.out.println("========="+string+"=========");

try {
    if (validator != null) {
            validator.validate(academic, errors);
            if(errors.hasErrors()){
                saveError(request, getText("academic.error",academic.getAcademicYear(),locale));
                return new ModelAndView("admin/academicForm", model);   
            }
        }
 }

that string prints correct format as ("dd/mm/yyyy)

Error :

Failed to convert property value of type java.lang.String to required type java.util.Date for property fromDate; nested exception is java.lang.IllegalArgumentException: Could not parse date: Unparseable date: "26/01/2015"

Please Help.

Use DateFormatter in the method initBinder in your Controller :

@InitBinder
public void initBinder(WebDataBinder binder) {
    binder.addCustomFormatter(new DateFormatter("dd/MM/yyyy","academic.fromDate" ));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM