2

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 ?

quirell
  • 245
  • 1
  • 18

3 Answers3

1

Add format to registerCustomEditor and try :

 SimpleDateFormat format = new SimpleDateFormat("Required format");
 webDataBinder.registerCustomEditor(Date.class, dateEditor, new CustomDateEditor(format, true));
Prasanna Kumar H A
  • 3,341
  • 6
  • 24
  • 52
1

Solved by using PropertyEditorSupport instead of CustomDateEditor as in Set date format for an input text using Spring MVC

Community
  • 1
  • 1
quirell
  • 245
  • 1
  • 18
0

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));
atish shimpi
  • 4,873
  • 2
  • 32
  • 50