Loading...
Spring Framework Reference Documentation 7.0.2의 Configuring a Global Date and Time Format의 한국어 번역본입니다.
아래의 경우에 피드백에서 신고해주신다면 반영하겠습니다.
감사합니다 :)
기본적으로 @DateTimeFormat으로 어노테이션되지 않은 date와 time field는
DateFormat.SHORT style을 사용하여 문자열에서 변환됩니다. 원한다면,
자신만의 global format을 정의하여 이를 변경할 수 있습니다.
이를 위해서는 Spring이 기본 포매터를 등록하지 않도록 해야 합니다. 대신, 다음의 도움을 받아 포매터를 수동으로 등록하십시오:
org.springframework.format.datetime.standard.DateTimeFormatterRegistrarorg.springframework.format.datetime.DateFormatterRegistrar예를 들어, 다음 설정은 global yyyyMMdd format을 등록합니다:
1@Configuration 2public class ApplicationConfiguration { 3 4 @Bean 5 public FormattingConversionService conversionService() { 6 7 // Use the DefaultFormattingConversionService but do not register defaults 8 DefaultFormattingConversionService conversionService = 9 new DefaultFormattingConversionService(false); 10 11 // Ensure @NumberFormat is still supported 12 conversionService.addFormatterForFieldAnnotation( 13 new NumberFormatAnnotationFormatterFactory()); 14 15 // Register JSR-310 date conversion with a specific global format 16 DateTimeFormatterRegistrar dateTimeRegistrar = new DateTimeFormatterRegistrar(); 17 dateTimeRegistrar.setDateFormatter(DateTimeFormatter.ofPattern("yyyyMMdd")); 18 dateTimeRegistrar.registerFormatters(conversionService); 19 20 // Register date conversion with a specific global format 21 DateFormatterRegistrar dateRegistrar = new DateFormatterRegistrar(); 22 dateRegistrar.setFormatter(new DateFormatter("yyyyMMdd")); 23 dateRegistrar.registerFormatters(conversionService); 24 25 return conversionService; 26 } 27}
1@Configuration 2class ApplicationConfiguration { 3 4 @Bean 5 fun conversionService(): FormattingConversionService { 6 // Use the DefaultFormattingConversionService but do not register defaults 7 return DefaultFormattingConversionService(false).apply { 8 9 // Ensure @NumberFormat is still supported 10 addFormatterForFieldAnnotation(NumberFormatAnnotationFormatterFactory()) 11 12 // Register JSR-310 date conversion with a specific global format 13 val dateTimeRegistrar = DateTimeFormatterRegistrar() 14 dateTimeRegistrar.setDateFormatter(DateTimeFormatter.ofPattern("yyyyMMdd")) 15 dateTimeRegistrar.registerFormatters(this) 16 17 // Register date conversion with a specific global format 18 val dateRegistrar = DateFormatterRegistrar() 19 dateRegistrar.setFormatter(DateFormatter("yyyyMMdd")) 20 dateRegistrar.registerFormatters(this) 21 } 22 } 23}
1<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> 2 <property name="registerDefaultFormatters" value="false" /> 3 <property name="formatters"> 4 <set> 5 <bean class="org.springframework.format.number.NumberFormatAnnotationFormatterFactory" /> 6 </set> 7 </property> 8 <property name="formatterRegistrars"> 9 <set> 10 <bean class="org.springframework.format.datetime.standard.DateTimeFormatterRegistrar"> 11 <property name="dateFormatter"> 12 <bean class="org.springframework.format.datetime.standard.DateTimeFormatterFactoryBean"> 13 <property name="pattern" value="yyyyMMdd"/> 14 </bean> 15 </property> 16 </bean> 17 </set> 18 </property> 19</bean>
web 애플리케이션에서 date와 time format을 설정할 때는 추가로 고려해야 할 사항이 있습니다. 자세한 내용은 WebMVC Conversion and Formatting 또는 WebFlux Conversion and Formatting을 참조하십시오.
Spring Field Formatting
Java Bean Validation