SpringMVC- date formatting

Page to submit data format If not, will report 400

1. Use annotations, add annotations in the date field above: single date Conversion

@DateTimeFormat(pattern="yyyy-MM-dd HH:mm")
public class Member implements Serializable {
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date birthday ;
public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }

2. Define an abstract parent class, subclass can inherit the parent class: Batch conversion date

public abstract class AbstractAction {
    private static final DateTimeFormatter LOCAL_DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd") ;
    @InitBinder
    public  void of an initBinder (WebDataBinder Binder) {   // This conversion process setting binding 
        binder.registerCustomEditor (java.util.Date. class , new new PropertyEditorSupport () {
            @Override
            public void setAsText(String text) throws IllegalArgumentException {
                LocalDate the LocalDate = LocalDate.parse (text, LOCAL_DATE_FORMAT); // Set the date local instance 
                ZoneID ID of ZoneID ID of = ZoneId.systemDefault ();
                Instant the Instant = . LocalDate.atStartOfDay () atZone (ZoneID ID of) .toInstant ();
                 Super .setValue (java.util.Date.from (Instant)); // string conversion date 
            }
        });
    }
}

 

Guess you like

Origin www.cnblogs.com/luliang888/p/11075090.html