3.6 전역 Date and Time 포맷 구성 by sh

기본적으로 @DateTimeFormat으로 어노테이션 처리되지 않은 날짜 및 시간 필드는 DateFormat.SHORT 스타일을 사용하여 문자열에서 변환됩니다. 원하는 경우 자체 전역 형식을 정의하여 이를 변경할 수 있습니다.

그렇게 하기 위해서는, 스프링이 기본 Formatter를 등록하지 않도록 해야합니다. 대신, 모든 Formatter를 수동으로 등록해야 합니다. Joda-Time 라이브러리를 사용하는 지 여부에 따라 org.springframework.format.datetime.joda.JodaTimeFormatterRegistrar 또는 ororg.springframework.format.datetime.DateFormatterRegistrar클래스를 사용하십시오.

예를 들어, 다음 자바 구성은 전역 yyyyMMdd형식을 등록합니다. (이 예제는 Joda-Time 라이브러리에 종속되지 않습니다)

@Configuration
public class AppConfig {

    @Bean
    public FormattingConversionService conversionService() {

        // Use the DefaultFormattingConversionService but do not register defaults
        DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService(false);

        // Ensure @NumberFormat is still supported
        conversionService.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory());

        // Register date conversion with a specific global format
        DateFormatterRegistrar registrar = new DateFormatterRegistrar();
        registrar.setFormatter(new DateFormatter("yyyyMMdd"));
        registrar.registerFormatters(conversionService);

        return conversionService;
    }
}

XML 기반 구성을 선호하는 경우 FormattingConversionServiceFactoryBean을 사용할 수 있습니다. 다음 예제에서는 Joda Time을 사용하여 이를 수행하는 방법을 보여줍니다.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd>

    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <property name="registerDefaultFormatters" value="false" />
        <property name="formatters">
            <set>
                <bean class="org.springframework.format.number.NumberFormatAnnotationFormatterFactory" />
            </set>
        </property>
        <property name="formatterRegistrars">
            <set>
                <bean class="org.springframework.format.datetime.joda.JodaTimeFormatterRegistrar">
                    <property name="dateFormatter">
                        <bean class="org.springframework.format.datetime.joda.DateTimeFormatterFactoryBean">
                            <property name="pattern" value="yyyyMMdd"/>
                        </bean>
                    </property>
                </bean>
            </set>
        </property>
    </bean>
</beans>

Joda-Time은 date, timedate-time값을 나타내는 별도의 고유한 타입을 제공합니다. JodaTimeFormatterRegistrardateFormatter, timeFormatterdateTimeFormatter속성을 사용하여 각 타입에 대해 다른 Format을 구성해야 합니다. DateTimeFormatterFactoryBean은 Formatter를 만드는 편리한 방법을 제공합니다.

스프링 MVC를 사용한다면, 명시적으로 사용되는 변환 서비스를 설정해야 합니다. 자바 기반의 @Configuration의 경우에는 WebMvcConfigurationSupport클래스를 확장하고 mvcConversionService() 메서드를 재 정의하는 것을 의미합니다. XML의 경우 mvc:annotation-driven요소의 conversion-service속성을 사용해야 합니다. 자세한 내용은 Conversion and Formatting을 참조하십시오

Last updated