SpringMVC中时间转换问题
SpringMVC中做时间转换时,总是取不到时间数据
情景: 在SpringMVC中本来以web默认时间格式2019/1/24传值时可以在Controller中取得时间数据,但在Controller中做了时间格式处理(对web中传来的值)时,在Controller中总是取不到时间数据。
Controller中对时间的处理:
jsp传值页面:
jsp显示数据页面:
解决办法:
最终调试良久才知把Date包导错了,导入的时间包为java.sql.Date,当换成java.util.Date时问题解决
jsp数据显示页面为:
附上时间转换代码
@Override
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) throws Exception {
String str = request.getParameter("birthday");
if(str.contains("/")) {
binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy/MM/dd"),true));
}else {
binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
}
}