Java 不可解析字符串:[不可解析日期:“Wed May 29 16:34:58 ART 2013”]仅在远程服务器上

Java 不可解析字符串:[不可解析日期:“Wed May 29 16:34:58 ART 2013”]仅在远程服务器上,java,linux,spring,spring-mvc,Java,Linux,Spring,Spring Mvc,我们有 一台运行在Jboss EAP6/Windows/US上的服务器A 另一台运行在Jboss EAP6/Linux/South America上的服务器B 当前的spring应用程序有一个传递日期选择框的UI页面,当单击submit时,这个日期对象将作为JavaBean的字段传递到下一个页面 现在的情况是: 服务器A可以正常运行此表单,但服务器B在提交时引发异常: nested exception is java.lang.IllegalArgumentException: Unparse

我们有

  • 一台运行在Jboss EAP6/Windows/US上的服务器A
  • 另一台运行在Jboss EAP6/Linux/South America上的服务器B
  • 当前的spring应用程序有一个传递日期选择框的UI页面,当单击submit时,这个日期对象将作为JavaBean的字段传递到下一个页面

    现在的情况是:

    服务器A可以正常运行此表单,但服务器B在提交时引发异常:

    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"]]
    
    似乎服务器B不知道如何处理数据格式,因为
    Wed May 29 16:34:58 ART 2013
    ,即使我添加了@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
    引用自文章

    有趣的是,当日期对象是bean的一个字段时,上述问题就会发生

    public String showSecondView(Form aForm,
            Model uiModel) {
        .....
    }
    
    但是,在另一个没有@InitBinder的控制器中,这是可以正常工作的

    public String list(Model uiModel, 
            @RequestParam(value = "fromDate", required = false) Date fromrDate,
              .....)
           ....
        }
    
    但是为什么即使使用@initBinder,这个错误仍然会发生?我以前做过,似乎平台有不同的方式来翻译时区代码,但Spring,我认为它能够支持国际化,对吗?

    很好

    问题主要是由于
    字符串
    日期
    的转换,它是

  • DateFormat
    对象中定义的格式与日期字符串值不匹配,或
  • 当前服务器环境无法通过其当前区域设置识别时区代码或其他时间格式
  • 所以

    在我的例子中,由于我们使用的是Spring框架,所以问题的原因是: 我并没有100%地遵循so

    }

    问题就在这里

    DateFormat sortFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy"); // AFTER REMOVE LOCALE
    
    我从sortFormat初始化中删除了
    LocaleContextHolder.getLocale()
    ,然后它就像一个符咒一样工作。当表单将日期字符串从UI返回到控制器时,我们似乎必须摆脱System local来转换日期字符串

    之所以可以,是因为printFormat在将日期信息放入UI时已经考虑了区域设置

    DateFormat printFormat = new SimpleDateFormat(DateTimeFormat.patternForStyle("S-", LocaleContextHolder.getLocale())); // format for joda time dojo UI
    
    因此,一旦日期字符串从UI返回,就不必在任何时候使用locale进行处理,我们可以为sortFormat提取locale。我猜是这样的

    干杯

    DateFormat sortFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy"); // AFTER REMOVE LOCALE
    
    DateFormat printFormat = new SimpleDateFormat(DateTimeFormat.patternForStyle("S-", LocaleContextHolder.getLocale())); // format for joda time dojo UI