1

After searching and looking in spring source code, I found I can configure application-wide default LocalTime formatter (mainly for use in Request Parameters) in one of two ways (both by subclasses for WebMvcConfigurer:

@Override
public void addFormatters(FormatterRegistry registry) {
    registry.addFormatterForFieldType(LocalTime.class,
            (object, locale) -> ISO_LOCAL_TIME.format((LocalTime) object),
            (text, locale) -> LocalTime.parse(text));
}

The other way is by redefining the DateTimeFormatterRegistrar itself and ask itself to use the iso format:

@Override
public void addFormatters(FormatterRegistry registry) {
    DateTimeFormatterRegistrar dateTimeFormatterRegistrar = new DateTimeFormatterRegistrar();
    dateTimeFormatterRegistrar.setUseIsoFormat(true);
    dateTimeFormatterRegistrar.registerFormatters(registry);
}

Which one is considered best practice, and won't spring team to add a property like the one provided for the date spring.mvc.date-format to be used for time?

Edit: doc says to use the first approach to register a new formatter; i think this is not the case here.

Muhammad Hewedy
  • 29,102
  • 44
  • 127
  • 219

0 Answers0