在Spring MVC中将默认/全局日期格式设置为ISO 8601 [英] Set default/global date format in Spring MVC to ISO 8601
问题描述
我有一个简单的Spring控制器:
I have a simple Spring Controller:
@RequestMapping(value="", method=RequestMethod.GET)
public void search(MyDTO dto) {
// ...
}
和 MyDTO
:
public class MyDTO {
private DateTime date;
public DateTime getDate() { return date; }
public void setDate(DateTime date) { this.date = date; }
}
我实际上可以使用本地日期格式调用控制器方法: 03.10.2013 01:00
,例如获取http:// localhost:8080 / test?date = 03.10.2013 01:00
I actually can call the controller method with my local date format: 03.10.2013 01:00
, e.g. GET http://localhost:8080/test?date=03.10.2013 01:00
但是我想要应用程序范围内的ISO 8601日期格式,例如: 2007-03-01T13:00:00Z
But I want application wide ISO 8601 date format, e.g.: 2007-03-01T13:00:00Z
如果使用ISO格式我收到以下错误:
If I use ISO format I get the following error:
Failed to convert property value of type 'java.lang.String' to required type
'org.joda.time.DateTime' for property 'date'; nested exception is
org.springframework.core.convert.ConversionFailedException: Failed to convert
from type java.lang.String to type org.joda.time.DateTime for value
'2013-09-25T23:05:18.000+02:00'; nested exception is
java.lang.IllegalArgumentException: Invalid format:
"2013-09-25T23:05:18.000+02:00" is malformed at "13-09-25T23:05:18.000+02:00"
对于 java.util,必须有某种更改方法。日期
以及所有这些Joda日期和时间容器。
There must be some way to change it for java.util.Date
and also all those Joda Date and Time containers.
我刚刚找到了 addFormatters(FormatterRegistry注册表)
WebMvcConfigurationSupport 中的方法,但我真的不知道如何使用它。
I just found the addFormatters(FormatterRegistry registry)
method within WebMvcConfigurationSupport
, but I don't really know how to use it.
推荐答案
我为乔达时间工作了:
public class WebConfig extends WebMvcConfigurationSupport {
@Override
public void addFormatters(FormatterRegistry registry) {
JodaTimeFormatterRegistrar j = new JodaTimeFormatterRegistrar();
j.setUseIsoFormat(true);
j.registerFormatters(registry);
}
}
我希望有一种更简单的方法可以完成此任务所有可能的Date实现。
I hope there's an easier way to get this done for ALL possible Date implementations.
最初由OP Benjamin M
这篇关于在Spring MVC中将默认/全局日期格式设置为ISO 8601的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!