Java 基于区域设置的Spring mvc自定义日期格式
这是我的控制器:Java 基于区域设置的Spring mvc自定义日期格式,java,spring-mvc,Java,Spring Mvc,这是我的控制器: @InitBinder protected void initBinder(final WebDataBinder binder) { DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, new Locale("lt")); dateFormat.setLenient(true); CustomDateEditor editor = new CustomDateEdit
@InitBinder
protected void initBinder(final WebDataBinder binder) {
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, new Locale("lt"));
dateFormat.setLenient(true);
CustomDateEditor editor = new CustomDateEditor(dateFormat, true);
binder.registerCustomEditor(Date.class, editor);
}
当我提交表格时,我收到一个错误,上面写着:
无法将java.lang.String类型的属性值转换为必需的
键入java.util.Date作为属性dateFrom;嵌套异常是
org.springframework.core.convert.ConversionFailedException:未能
将值的类型从java.lang.String转换为java.util.Date
2014-02-20; 嵌套异常是java.lang.IllegalArgumentException
如何解决此问题?您的解决方案不起作用,因为
lt
locale具有类似yy.M.d
dateFrom的值为2014-02-20,您的DateFormat
正在尝试使用以下日期模式进行转换
尝试在dateFrom参数中输入类似14.02.20的内容,应该可以使用。意大利默认日期格式缩写为dd/mm/yyyy。参考如果您以2014年2月20日的日期提交您的意见,您将表现良好 你也可以这样测试
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.ITALY);
dateFormat.setLenient(true);
CustomDateEditor editor = new CustomDateEditor(dateFormat, true);
editor.setAsText("20/02/2012");
或者使用SimpleDataFormat并指定您的格式。它是立陶宛语言环境,而不是意大利语。