-
Notifications
You must be signed in to change notification settings - Fork 38.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support fallback parsing patterns in @DateTimeFormat #20292
Comments
Is this ever to come? It would be useful to support multiple date patterns on the same field. |
How would you envision that working? Would Spring iterate over the patterns in declaration order and attempt to parse an incoming string using each |
That's how I would expect it, yes. As workaround for now, I registered a custom Formatter for @Configuration
public class Config implements WebMvcConfigurer {
@Override
pubic void addFormatters(FormatterRegistry r) {
r.addFormatterForFieldType(LocalDate.class, new LocalDateFormatter());
}
}
class LocalDateFormatter implements Formatter<LocalDate> {
private final DateTimeFormatter PATTERN_ISO = DateTimeFormatter.ofPattern("yyyy-MM-dd");
private final DateTimeFormatter PATTERN_DE = DateTimeFormatter.ofPattern("dd.MM.yyyy");
@Override
public LocalDate parse(String text, Locale locale) {
if (StringUtils.isBlank(text)) return null;
if (text.contains(".")) return LocalDate.parse(text, PATTERN_DE);
//some more patterns.
return LocalDate.parse(text, PATTERN_ISO);
}
} But it would be nice simply having the same feature by repeating annotations. |
I agree that such a feature would be "nice to have". Unfortunately, it does not appear to be straightforward to support... simply due to the fact that the entire infrastructure around |
After some brainstorming with the team, we have come up with an alternative approach to this: @DateTimeFormat(patterns = { "yyyy-MM-dd", "yyyyMMdd", "yyyy.MM.dd" })
private LocalDate date; That would be made possible via a new This alternative approach allows multiple patterns to be processed with the existing @membersound, what do you think about that? |
That's really a great idea. Well done! |
Current work on this can be viewed in my feature branch: master...sbrannen:issues/gh-20292-DateTimeFormat-patterns The current approach is to introduce a new An example taken from the test suite: @DateTimeFormat(pattern = "yyyy-MM-dd", fallbackPatterns = { "M/d/yy", "yyyyMMdd", "yyyy.MM.dd" })
private Date dateWithFallbackPatterns; |
The support for fallback parsing patterns in @DateTimeFormat introduced in gh-20292 introduced a regression in that the original cause of the parsing exception was no longer retained. This commit ensures that the original DateTimeParseException is set as the cause for any newly created DateTimeParseException, thereby retaining the original exception as the root cause. Closes gh-26777
The support for fallback parsing patterns in @DateTimeFormat introduced in gh-20292 introduced a regression in that the original cause of the parsing exception was no longer retained. gh-26777 addressed that regression for `java.time` support; whereas, this commit addresses that regression for legacy Date types. This commit ensures that the original ParseException is set as the cause for any newly created ParseException, thereby retaining the original exception as the root cause. Closes gh-26804
Prior to this commit, @DateTimeFormat only supported a single format for parsing date time values via the style, iso, and pattern attributes. This commit introduces a new fallbackPatterns attribute that can be used to configure multiple fallback patterns for parsing date time values. This allows applications to accept multiple input formats for date time values. For example, if you wish to use the ISO date format for parsing and printing but allow for lenient parsing of user input for various additional date formats, you could annotate a field or method parameter with configuration similar to the following. @DateTimeFormat( iso = ISO.DATE, fallbackPatterns = { "M/d/yy", "dd.MM.yyyy" } ) Closes spring-projectsgh-20292
The support for fallback parsing patterns in @DateTimeFormat introduced in spring-projectsgh-20292 introduced a regression in that the original cause of the parsing exception was no longer retained. This commit ensures that the original DateTimeParseException is set as the cause for any newly created DateTimeParseException, thereby retaining the original exception as the root cause. Closes spring-projectsgh-26777
The support for fallback parsing patterns in @DateTimeFormat introduced in spring-projectsgh-20292 introduced a regression in that the original cause of the parsing exception was no longer retained. spring-projectsgh-26777 addressed that regression for `java.time` support; whereas, this commit addresses that regression for legacy Date types. This commit ensures that the original ParseException is set as the cause for any newly created ParseException, thereby retaining the original exception as the root cause. Closes spring-projectsgh-26804
member sound opened SPR-15736 and commented
It would be nice if one could set multiple formats for:
https://docs.oracle.com/javase/tutorial/java/annotations/repeating.html
No further details from SPR-15736
The text was updated successfully, but these errors were encountered: