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

 

.

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM