SpringMvc receive date parameters

1. The Controller method receives the parameters through the @DateTimeFormat annotation

@RequestParam("startTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime

 2. Set InitBinder receive parameter 1

    @InitBinderpublic void initBinderQuery(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));
    }

3. Set InitBinder receiving parameter 2

Incoming value? 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. Annotate the parameter receiving entity

 Annotate @DateTimeFormat directly on the property to convert the date format

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date creatDate ;

    public Date getCreatDate() {
        return creatDate;
    }

    public void setCreatDate(Date creatDate) {
        this.creatDate = creatDate;
    }

 

.

 

Guess you like

Origin www.cnblogs.com/zsg88/p/12759893.html