Failed to convert from type [java.lang.String] to type [java.util.Date] for value ‘2019-11-07 23:41:54’; nested exception is java.lang.IllegalArgumentException]]
spring mvc数据绑定报错,时间字符串数据与Date类型转换方法:
方案一:
在对应的Controller控制器中
@InitBinder
protected void init(HttpServletRequest request, ServletRequestDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}
方案二:
在pojo里的Date属性上加上
@DateTimeFormat(pattern = “yyyy-MM-dd HH:mm:ss”)
例如:
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createtime;