기본적으로 @DateTimeFormat으로 어노테이션 처리되지 않은 날짜 및 시간 필드는 DateFormat.SHORT 스타일을 사용하여 문자열에서 변환됩니다. 원하는 경우 자체 전역 형식을 정의하여 이를 변경할 수 있습니다.
그렇게 하기 위해서는, 스프링이 기본 Formatter를 등록하지 않도록 해야합니다. 대신, 모든 Formatter를 수동으로 등록해야 합니다. Joda-Time 라이브러리를 사용하는 지 여부에 따라 org.springframework.format.datetime.joda.JodaTimeFormatterRegistrar 또는 ororg.springframework.format.datetime.DateFormatterRegistrar클래스를 사용하십시오.
예를 들어, 다음 자바 구성은 전역 yyyyMMdd형식을 등록합니다. (이 예제는 Joda-Time 라이브러리에 종속되지 않습니다)
@ConfigurationpublicclassAppConfig { @BeanpublicFormattingConversionServiceconversionService() {// Use the DefaultFormattingConversionService but do not register defaultsDefaultFormattingConversionService conversionService =newDefaultFormattingConversionService(false);// Ensure @NumberFormat is still supportedconversionService.addFormatterForFieldAnnotation(newNumberFormatAnnotationFormatterFactory());// Register date conversion with a specific global formatDateFormatterRegistrar registrar =newDateFormatterRegistrar();registrar.setFormatter(newDateFormatter("yyyyMMdd"));registrar.registerFormatters(conversionService);return conversionService; }}
XML 기반 구성을 선호하는 경우 FormattingConversionServiceFactoryBean을 사용할 수 있습니다. 다음 예제에서는 Joda Time을 사용하여 이를 수행하는 방법을 보여줍니다.
Joda-Time은 date, time및 date-time값을 나타내는 별도의 고유한 타입을 제공합니다. JodaTimeFormatterRegistrar의 dateFormatter, timeFormatter및 dateTimeFormatter속성을 사용하여 각 타입에 대해 다른 Format을 구성해야 합니다. DateTimeFormatterFactoryBean은 Formatter를 만드는 편리한 방법을 제공합니다.
스프링 MVC를 사용한다면, 명시적으로 사용되는 변환 서비스를 설정해야 합니다. 자바 기반의 @Configuration의 경우에는 WebMvcConfigurationSupport클래스를 확장하고 mvcConversionService() 메서드를 재 정의하는 것을 의미합니다. XML의 경우 mvc:annotation-driven요소의 conversion-service속성을 사용해야 합니다. 자세한 내용은 Conversion and Formatting을 참조하십시오