一:如果在一次请求中只有一种时间格式,则可以使用以下方法进行绑定:
@InitBinder
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) throws Exception {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(true);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
二:如果一次 请求中的参数有多种时间格式的话,可以使用注解的方式在model上进行参数的绑定,比如:
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date bumenfzrqzrq ;
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
private Date xiujiajssj ;
但是,请注意,如果使用第二种的话,需要添加一个joda-time-2.1.jar包,否则会报错的。下面分享一下这个jar包