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)
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)
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