在SpringMVC中解析页面提交的请求参数时,日期默认的格式是yyyy/MM/dd,并不符合中国人平常的使用习惯,此时可以配置适配器自己来指定格式
//日期格式转换,默认只支持yyyy/MM/dd格式,要转变为yyyy-MM-dd
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//注册一个自定义格式转换器
binder.registerCustomEditor(Date.class, new CustomDateEditor(df,true));
}
//注解方式实现
@InitBinder
public void InitBinder (ServletRequestDataBinder binder){
binder.registerCustomEditor(
java.util.Date.class,
new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
}