【问题标题】:Spring Date conversion error while submiiting form提交表单时出现春季日期转换错误
【发布时间】:2017-05-28 17:50:39
【问题描述】:

我正在使用以下代码设置日期格式:

@InitBinder
public void initBinder(final WebDataBinder binder) {
    binder.initDirectFieldAccess();

    final SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    dateFormat.setLenient(false);
    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}

并从 jsp 以相同格式发送日期,但出现以下错误:

无法将类型 [Java.Lang.String] 的属性值转换为属性 BidDate 所需的类型 [Java.Util.Date];嵌套异常是 Java.Lang.IllegalArgumentException:无法解析日期:无法解析日期:“05/28/2017”

【问题讨论】:

    标签: java spring-mvc model-view-controller


    【解决方案1】:

    您的日期格式是 dd/MM/yyyy,但您传递给它的是 MM/dd/yyyy 日期 (05/28/2017)

    【讨论】:

      【解决方案2】:

      您的 JSP 中的日期格式与班级中指定的日期格式不同,这意味着您的 JSP 发送 05/28/2017 (MM/dd/yyyy) 并且您的班级正在等待格式 @ 987654322@.

      所以,你可以尝试改变这个:

      final SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
      

      为此:

      final SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
      

      或者反过来,您可以在 JSP

      中更改日期格式

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-08-09
        • 1970-01-01
        • 1970-01-01
        • 2014-08-01
        • 1970-01-01
        • 2015-08-09
        • 1970-01-01
        相关资源
        最近更新 更多