Failed to check date in Spring MVC

I use custom editors to convert String to date. My code is below

@InitBinder public void initBinder(WebDataBinder binder) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); dateFormat.setLenient(false); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false)); } 

When I enter the date in "yyyy-MM-dd", then insert it.

But when I enter the empty, it throws an exception

The nested exception is java.lang.IllegalArgumentException: Failed to parse date: Unmatched date: ""

I can not confirm this.

If I give some other format, it will also not be able to parse.

+4
source share
1 answer

When you create an instance of CustomDateEditor, the second parameter in your case should be true. True allows null values.

CustomDateEditor (SpringSource)

+4
source

Source: https://habr.com/ru/post/1339633/


All Articles