Reputation: 255
I am trying to register custom date editor in spring's MVC WebDataBinder to make spring parse my custom date fomat (to be exact, it's ISO format). I succeded with that by implementing CustomWebBindingInitializer.
public static class CustomWebBindingInitializer implements WebBindingInitializer {
@Override
public void initBinder(WebDataBinder webDataBinder, WebRequest webRequest) {
CustomDateEditor dateEditor = new CustomDateEditor(new ISODateFormat(), true);
webDataBinder.registerCustomEditor(Date.class, dateEditor);
}
}
Spring is using my editor and parses date successfully , but nontheless date field doesn't get bound and I get following error for the request:
"org.springframework.validation.BindException",,"defaultMessage":"Failed to convert property value of type [java.lang.String] to required type [java.util.Date] for property 'from'; nested exception is java.lang.IllegalArgumentException: Could not parse date: Unparseable date: \"2016-08-01T10:35:04.126Z\""
What's worth noting: when I use default spring's format MM/DD/YYYY with my custom editor I get the same error, which means that spring is using my editor instead of default one.
when I use default spring's parser with format MM/DD/YYYY everything works and date gets bound, which is obvious of course.
Anyone had the same issue ?
Upvotes: 2
Views: 788
Reputation: 5023
Create SimpleDateFormat
with required date format like "yyyy-MM-dd"
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Register CustomDateEditor
with required dateForat
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
Upvotes: 0
Reputation: 255
Solved by using PropertyEditorSupport instead of CustomDateEditor as in Set date format for an input text using Spring MVC
Upvotes: 1
Reputation: 3431
Add format to registerCustomEditor
and try :
SimpleDateFormat format = new SimpleDateFormat("Required format");
webDataBinder.registerCustomEditor(Date.class, dateEditor, new CustomDateEditor(format, true));
Upvotes: 1