简体   繁体   中英

How to bind date and time separately?

I want to bind date and time separately in a controller. I have 1 timepicker and 1 datepicker on a form, i use InitBinder binder for time picker which is,

@InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setLenient(true);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }

But this doesn't work for both time picker and date picker. any better suggestion please....

Try this with your code:

@InitBinder
void allowFields(WebDataBinder webDataBinder){
    webDataBinder.setAllowedFields("date"); 
    webDataBinder.setAllowedFields("time");
}

and use this in your POJO:

  @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
  private Date date;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM