Configuring a Global Date and Time Format

By default, date and time fields not annotated with @DateTimeFormat are converted from strings by using the DateFormat.SHORT style. If you prefer, you can change this by defining your own global format.

To do that, ensure that Infra does not register default formatters. Instead, register formatters manually with the help of:

  • cn.taketoday.format.datetime.standard.DateTimeFormatterRegistrar

  • cn.taketoday.format.datetime.DateFormatterRegistrar

For example, the following configuration registers a global yyyyMMdd format:

@Configuration
public class ApplicationConfiguration {

  @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 JSR-310 date conversion with a specific global format
    DateTimeFormatterRegistrar dateTimeRegistrar = new DateTimeFormatterRegistrar();
    dateTimeRegistrar.setDateFormatter(DateTimeFormatter.ofPattern("yyyyMMdd"));
    dateTimeRegistrar.registerFormatters(conversionService);

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

    return conversionService;
  }
}

Note there are extra considerations when configuring date and time formats in web applications. Please see WebMVC Conversion and Formatting.