1.Controller方法通過@DateTimeFormat注解來接收參數
@RequestParam("startTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime
2.設置InitBinder接收參數1
@InitBinderpublic void initBinderQuery(WebDataBinder binder) { binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true)); }
3.設置InitBinder接收參數2
傳入值?query.startTime=2020-04-23
@InitBinder("query") public void initBinderQuery(WebDataBinder binder) { binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true)); binder.setFieldDefaultPrefix("query."); }
4. 在參數接收實體上注解
直接在屬性上加@DateTimeFormat注解,就可以進行轉化日期格式
@DateTimeFormat(pattern = "yyyy-MM-dd") private Date creatDate ; public Date getCreatDate() { return creatDate; } public void setCreatDate(Date creatDate) { this.creatDate = creatDate; }
.