2013-10-02 152 views
5

我有一个简单的Spring控制器:设置默认/全球日期格式在Spring MVC到ISO 8601

@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,例如GET http://localhost:8080/test?date=03.10.2013 01:00

但我想申请宽广的ISO 8601日期格式,如:2007-03-01T13:00:00Z

如果我使用ISO格式,我收到以下错误:

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.Date以及所有Joda Date和Time容器。

我刚刚在WebMvcConfigurationSupport内发现了addFormatters(FormatterRegistry registry)方法,但我真的不知道如何使用它。

回答

0

我得到它的工作为乔达时间:

public class WebConfig extends WebMvcConfigurationSupport { 
    @Override 
    public void addFormatters(FormatterRegistry registry) { 
     JodaTimeFormatterRegistrar j = new JodaTimeFormatterRegistrar(); 
     j.setUseIsoFormat(true); 
     j.registerFormatters(registry); 
    }  
} 

我希望有获得这个对所有可能的日期实现做了一个更简单的方法。

最初发布编辑由OP的问题,Benjamin M