Failed to convert property value of type ’java.lang.String‘ to required type ‘java.util.Date’…
程序员文章站
2024-01-14 16:39:52
...
Failed to convert property value of type ’java.lang.String‘ to required type ‘java.util.Date’…
问题描述
-
在日期对应属性上添加注解,JsonFormat能有用,数据从前端传输到后端时,无法识别DateTimeFormat失效
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
后台错误展示
解决方法一:
@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));
}
解决方法二:
public void setBtime(Date btime) {
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
String dateStr=simpleDateFormat.format(btime);
java.sql.Date sqlDate=null;
long time = 0;
try {
time = simpleDateFormat.parse(dateStr).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
sqlDate=new java.sql.Date(time);
this.btime = sqlDate;
}
上一篇: 这个重写规则是什么意思啊多谢
推荐阅读
-
Failed to convert property value of type ’java.lang.String‘ to required type ‘java.util.Date’…
-
Cannot convert value of type ‘java.lang.String‘ to required type ‘org.apache.ibatis.session.SqlSessi
-
解决重定向:Cannot convert value of type 'SysRole' to required type 'String'
-
java.lang.IllegalStateException: Cannot convert value of type ‘java.lang.String‘ to required type ‘c
-
SpringMVC自定义日期转换器不起作用,报400错误,Failed to convert value of type xxx to required type xxx
-
Failed to convert property value of type [java.lang.String] to required type [java.util.Date]
-
SpringMVC自定义日期转换器不起作用,报400错误,Failed to convert value of type xxx to required type xxx