복붙노트

[SPRING] Spring MVC에서 기본 / 글로벌 날짜 형식을 ISO 8601로 설정합니다.

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 날짜 및 시간 컨테이너에 대해이를 변경하는 방법이 있어야합니다.

방금 WebMvcConfigurationSupport 내에서 addFormatters (FormatterRegistry 레지스트리) 메소드를 찾았지만 실제로 사용하는 방법을 모르겠습니다.

해결법

  1. ==============================

    1.나는 Joda 시간 동안 그것을 작동하게했다 :

    나는 Joda 시간 동안 그것을 작동하게했다 :

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

    모든 가능한 Date 구현에 대해이 작업을 수행하는 더 쉬운 방법이 있기를 바랍니다.

    원래 OP의 질문에 대한 편집본으로 게시 됨, Benjamin M

  2. from https://stackoverflow.com/questions/19148649/set-default-global-date-format-in-spring-mvc-to-iso-8601 by cc-by-sa and MIT license