我使用以下代码设置日期格式:
@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“
发布于 2017-05-28 18:00:12
您的日期格式是dd/MM/yyyy,但您将它传递给MM/dd/yyyy日期(05/28/2017)。
发布于 2017-05-28 20:02:56
JSP提供的日期格式与类中指定的日期格式不同,这意味着您的JSP发送05/28/2017 (MM/dd/yyyy),而您的类正在等待格式28/05/2017 (dd/MM/yyyy)。
因此,您可以尝试更改以下内容:
final SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");为此:
final SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");也可以在JSP中更改日期格式。
https://stackoverflow.com/questions/44230303
复制相似问题