SpringMvc接收日期参数

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;
    }

.

猜你喜欢

转载自www.cnblogs.com/zsg88/p/12759893.html