Set default format for LocalDateTime (jsr310) in spring when passed to a rest end point

2.9k views Asked by At

I am trying to figure out the default java.time.LocalDateTime format when the user pass a date in the URL as a request parameter when used in conjunction with QueryDSL

I have Looked at QuerydslPredicateArgumentResolver, and ending with DateTimeFormatterRegistrar and found the default to read from:

private DateTimeFormatter getFormatter(Type type) {
    DateTimeFormatter formatter = this.formatters.get(type);
    if (formatter != null) {
        return formatter;
    }
    DateTimeFormatter fallbackFormatter = getFallbackFormatter(type);
    return this.factories.get(type).createDateTimeFormatter(fallbackFormatter);
}

private DateTimeFormatter getFallbackFormatter(Type type) {
    switch (type) {
        case DATE: return DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
        case TIME: return DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT);
        default: return DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
    }
}

So, the default value should be SHORT, however It seems not working as expected.

Also, how to override the format application-wide (not using the field-specific @DateTimeFormat annotation)

2

There are 2 answers

0
Muhammad Hewedy On

This solution not just for the date parameters that are used by QueryDSL, but for any Date parameters passed to spring (also can be used by to register any other formatter)

// This example uses ISO format for both java.util.Date classes and jsr310 classes, you can change the format to whatever suitable for your case
@Configuration
public static class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addFormatters(FormatterRegistry registry) {
        super.addFormatters(registry);

        DateFormatter javaUtilDateFormatter = new DateFormatter();
        javaUtilDateFormatter.setIso(ISO.DATE_TIME);

        DateTimeFormatter jsr310Formatter = DateTimeFormatter.ISO_DATE_TIME;

        DateFormatterRegistrar javaUtilDate = new DateFormatterRegistrar();
        javaUtilDate.setFormatter(javaUtilDateFormatter);
        javaUtilDate.registerFormatters(registry);

        DateTimeFormatterRegistrar jsr310 = new DateTimeFormatterRegistrar();
        jsr310.setDateTimeFormatter(jsr310Formatter);
        jsr310.setDateFormatter(jsr310Formatter);
        jsr310.setTimeFormatter(jsr310Formatter);
        jsr310.registerFormatters(registry);

    }
}

The class above register formatter for both java.util.date and jsr310 LocalDate/LocalTime/LocalDateTime, it can also be used to register formatters for joda datetime.

complete code example: https://github.com/project-seeds/spring-argument-resolver-parser

0
Viacheslav Petriaiev On

You could use this one code

@Configuration
public class DateTimeFormatConfiguration implements WebMvcConfigurer {

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

https://github.com/jhipster/jhipster-sample-app-hazelcast/blob/master/src/main/java/io/github/jhipster/sample/config/DateTimeFormatConfiguration.java