春季Java 8时间API的日期验证
我们都知道春季Java 8时间API的日期验证,java,spring,java-8,date-format,Java,Spring,Java 8,Date Format,我们都知道initBinder方法中控制器中日期验证的经典示例: @InitBinder public void initBinder(WebDataBinder binder) { SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm"); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)
initBinder
方法中控制器中日期验证的经典示例:
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm");
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
但是,支持新的Java8时间API的替代方案是什么,我们需要将DateFormat
替换为DateTimeFormatter
?Spring框架为此提供了哪些工具
谢谢。binder.registerCustomEditor方法还不支持DateTimeFormatter,但在Spring 4中,您可以通过在pojo中使用@DateTimeFormat和Java 8日期时间(Java.Time)对象来实现这一点
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
}
如需进一步参考,请访问