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