0

We have

  1. One server A running on Jboss EAP6/Windows/US
  2. Another server B running on Jboss EAP6/Linux/South America

Current spring application, has a UI page that pass a Date select box, when click submit, this date object will pass to the next page as a field of a java bean.

Now the situation is:

Server A runs this form fine without problem, but server B throw exception when submit:

nested exception is java.lang.IllegalArgumentException: 
Unparseable string: [Unparseable date: "Wed May 29 16:34:58 ART 2013", 
Unparseable date: "Wed May 29 16:34:58 ART 2013"]]

Seems server B doesn't know how to process the data format as Wed May 29 16:34:58 ART 2013, even I add an @initBinder

@InitBinder
public void registerDateBinder(WebDataBinder binder) {
    DateFormat printFormat = new SimpleDateFormat(DateTimeFormat.patternForStyle("S-", LocaleContextHolder.getLocale())); // format for joda time dojo UI
    printFormat.setLenient(false);
    DateFormat sortFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy" , LocaleContextHolder.getLocale()); // format for whatever return from form
    sortFormat.setLenient(false);
    binder.registerCustomEditor(Date.class, new ExpandableCustomDateEditor(printFormat, Arrays.asList(printFormat, sortFormat), true));
}

ExpandableCustomDateEditor is referred from this article

Interesting part is above issue happens when that Date object is a field of the bean

public String showSecondView(Form aForm,
        Model uiModel) {
    .....
}

But this works without problem in another controller without @InitBinder

public String list(Model uiModel, 
        @RequestParam(value = "fromDate", required = false) Date fromrDate,
          .....)
       ....
    }

But how come this error still happens, even with that @initBinder? I have made post before and seems platform has different way to translate timezone code, but Spring, I think it is capable to support internationalization right?

Community
  • 1
  • 1
Dreamer
  • 7,333
  • 24
  • 99
  • 179

1 Answers1

0

Nailed it.

The issue is mainly because of String to Date conversion, it is either

  1. the format defined in DateFormat object does not match date String value, or
  2. Current server environment cannot identify the timezone code or other time format by its current locale

So

solution to issue1: find the correct Date format, google is best friend
solution to issue2: remove locale from current `DateFormat` object, only use
    DateFormat df = new DateFormat(String pattern);
    instead of
    DateFormat df = new DateFormat(String pattern, Locale aLocale);
    when convert String to Date from what return from form

In my case, as we are on Spring framework so the reason of the problem is: I did not quite 100% follow the post so

@InitBinder
public void registerDateBinder(WebDataBinder binder) {
    DateFormat printFormat = new SimpleDateFormat(DateTimeFormat.patternForStyle("S-", LocaleContextHolder.getLocale())); // format for joda time dojo UI
    printFormat.setLenient(false);
    DateFormat sortFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy" , LocaleContextHolder.getLocale()); // NO LOCALE PlEASE!!!!!!
    sortFormat.setLenient(false);
    binder.registerCustomEditor(Date.class, new ExpandableCustomDateEditor(printFormat, Arrays.asList(printFormat, sortFormat), true));

}

the problem is here

DateFormat sortFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy"); // AFTER REMOVE LOCALE

I remove the LocaleContextHolder.getLocale() from sortFormat initiation then it works like a charm. It seems we have to get rid of System local to convert the date String when the form return date string back from UI to the controller.

The reason it is ok is because the printFormat already take care of locale when put date information to UI

DateFormat printFormat = new SimpleDateFormat(DateTimeFormat.patternForStyle("S-", LocaleContextHolder.getLocale())); // format for joda time dojo UI

so once the Date string get back from the UI, it is not necessary to process with locale any fore and it should be ok we take out the locale for sortFormat. That's what I guess.

Cheers.

Dreamer
  • 7,333
  • 24
  • 99
  • 179