方案一:
在controller类中增加如下:
@InitBinder
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
simpleDateFormat.setLenient(false);
CustomDateEditor dateEditor = new CustomDateEditor(simpleDateFormat, true);
binder.registerCustomEditor(Date.class, dateEditor);
}
修改springmvc.xml
mvc:annotation-driven</mvc:annotation-driven>
页面修改
<input type="date" name="timedate" />
这段代码后台接收到的形式可以是String ,也可以是 Date
String 的值是:2021-09-15
Date 的值是:Wed Sep 15 00:00:00 CST 2021
想在页面回显Date 类型
- <%@ taglib uri=“http://java.sun.com/jsp/jstl/fmt” prefix=“fmt”%>
- <input type=“date” value="<fmt:formatDate value="${times}" pattern=“yyyy-MM-dd” />">
<input type="datetime-local" name="timelocal" />
这段代码后台接收到的可以是String 或Date
String的值:2021-09-15T15:15
Date的值:Wed Sep 15 00:00:00 CST 2021
这个T 在Edge 和谷歌浏览器都有这个问题,要么处理以下,处理方案如下:
public String testReplace(String time){
String times = time.substring(0,10)+" "+time.substring(11,16);
return times ;
}
public String testReplace2(String time){
String times = time.replace("T"," ");
return times ;
}