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

给你一囗甜甜゛ 提交于 2019-12-03 23:10:42

Add this in the controller. Change the dateFormat to your locale preference.

@InitBinder     
public void initBinder(WebDataBinder binder){
     binder.registerCustomEditor(       Date.class,     
                         new CustomDateEditor(new SimpleDateFormat("dd/MM/yyyy"), true, 10));   
}

Consider using this annotations in your model (Adapting format and TemporalType for your preferences)

@DateTimeFormat(pattern = "dd/MM/yyyy")
@Temporal(TemporalType.DATE)

simply add @DateTimeFormat(pattern = "yyyy-MM-dd") with (-)

example:

@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date dateNaissance;

You need to change in BaseController for initBinder

@InitBinder
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");   
    dateFormat.setLenient(false);
    binder.registerCustomEditor(Date.class, null,  new CustomDateEditor(dateFormat, true));
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!