在springboot控制器绑定日期验证器时,发现转换后日期总是上送日期前一天并且带T16这样子的字眼。大概如下,上传为:
date=2018-03-02
输出时成了
2018-03-01T16:00:00.000+00:00。
网上查找是因为json处理的时候没有格式化。解决方案为:在日期属性上加上注解:
@JsonFormat(pattern = "yyyy-MM-dd")
由于我日期是java.util.Date,而正好它的子类java.sql.Data时间格式正好没有时分秒,即没有Time,将Date导入换为java.sql.Data,也可以完美解决问题。
绑定日期验证器代码如下:
@InitBinder public void initBinder(WebDataBinder binder) { //绑定验证器 binder.setValidator(new UserValidator()); //定义日期格式参数,参数不再需要注解@DateTimeFormat,boolean参数表示是否允许为空 binder.registerCustomEditor(Date.class,new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),false)); }