2016-12-02 12 views
1
は、我々は、すべての initBinderメソッド内のコントローラで日付の検証の古典的な例を知って

日付検証は

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

しかし、新しいJavaの8時間をサポートするための代替するものですAPIDateFormatDateTimeFormatterに置き換える必要がありますか? Spring Frameworkはこれに対してどんなツールを提供していますか?

ありがとうございました。

答えて

3

binder.registerCustomEditorメソッドはDateTimeFormatterをまだサポートしていませんが、Spring 4では、あなたのpojoにJava 8 Date-Time(java.time)オブジェクトを使用して@DateTimeFormatを使用してこれを実現できます。さらに、参照用

public class MyPojo { 

    @DateTimeFormat(iso = ISO.DATE) 
    private LocalDate localDate1; 

    // you can use pattern as well 
    @DateTimeFormat(pattern = "dd.MM.yyyy HH:mm") 
    private LocalDate localDate2; 

    // setters & getters 
} 

http://blog.codeleak.pl/2014/06/spring-4-datetimeformat-with-java-8.html

http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/format/annotation/DateTimeFormat.html

をご覧ください。