[英]Error: Failed to convert property value of type java.lang.String to required type java.util.Date for property dob?
I am trying simple Spring-MVC application for form validations. 我正在尝试用于表单验证的简单Spring-MVC应用程序。 I am facing an issue while taking date input from browser. 从浏览器获取日期输入时遇到问题。 The error is: 错误是:
"Failed to convert property value of type java.lang.String to required type java.util.Date for property dob;
nested exception is java.lang.IllegalArgumentException: Could not parse date: Unparseable date: "2015-12-25"
This problem I am facing in Google Chrome browser only. 我仅在Google Chrome浏览器中遇到此问题。 In internet explorer and eclipse interface its working fine. 在Internet Explorer和Eclipse界面中,其工作正常。 I am using Chrome-40.0.2214.115m. 我正在使用Chrome-40.0.2214.115m。
I am sharing part of my code here: 我在这里分享我的部分代码:
From Controller file: 从控制器文件:
...
@InitBinder
public void initBinder(WebDataBinder binder){
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
binder.registerCustomEditor(Date.class,new CustomDateEditor(dateFormat, false));
}
...
From JSP file: 从JSP文件:
...
<tr>
<td><label for="dob">DOB</label></td>
<td><input type="date" name="dob" /></td>
<td><form:errors path="person1.dob" /></td>
</tr>
...
If there is problem with browser compatibility, then what is the solution for it? 如果浏览器兼容性存在问题,那么解决方案是什么? Please help me to solve this issue. 请帮我解决这个问题。
Input tag date format is always like YYYY-MM-DD
. 输入标签日期格式始终类似于YYYY-MM-DD
。 So change your format pattern while formatting to SimpleDateFormat
with this yyyy-MM-dd
. 因此,在使用yyyy-MM-dd
格式化为SimpleDateFormat
,请更改您的格式模式。
So try this one 所以试试这个
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.