1.在controller添加方法
@InitBinder protected void initDateFormatBinder(WebDataBinder binder) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); /*SimpleDateFormat dateFormatTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormatTime, true));*/ }
缺点:只能支持 yyyy-MM-dd 或者 yyyy-MM-dd HH:mm:ss
2.使用字符串接收,后端自己转
3.在接收对象上设置注解
@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8") //返回前端自动转json格式 @DateTimeFormat(pattern="yyyy-MM-dd") //接收参数 前端为该格式自动封装到对象 @Column(name = "birthd") //数据库列名 private Date birthd;
该内容来源于网络,仅供参考