spring-framework icon indicating copy to clipboard operation
spring-framework copied to clipboard

DateTimeFormatterRegistrar should use ISO formats by default

Open abccbaandy opened this issue 4 years ago • 5 comments

Affects: 5.3.7

Currently DateTimeFormatterRegistrar behavior is different from default JDK

For example, given OffsetDateTime.parse(XXX), if XXX can be parsed this way, it should be parsed by DateTimeFormatterRegistrar too.

abccbaandy avatar May 27 '21 10:05 abccbaandy

Please note that changing the current behavior would be a breaking change.

The documentation for DateTimeFormatterRegistrar states that it does not use ISO formatting by default, but that can be changed via DateTimeFormatterRegistrar#setUseIsoFormat(true).

Can you please provide more information regarding your exact use case?

sbrannen avatar May 27 '21 12:05 sbrannen

Yes, I know setUseIsoFormat. I create this issue to discussion is there any chance to change it to true by default.

The reason I already said in first comment. This option should be true by default.

About my real use case : I use RFC 3339 format in all time field in my rest api. It work well with json(jackson) without any config, the default config is good and standard. But when use it in query parameter, it fail.

abccbaandy avatar May 27 '21 12:05 abccbaandy

But when use it in query parameter, it fail.

Have you tried annotating the corresponding parameter of your controller method with @DateTimeFormat(iso = ISO.DATE_TIME)?

sbrannen avatar May 27 '21 12:05 sbrannen

Yes, I can do that, but I want make it default without any config.

abccbaandy avatar May 27 '21 12:05 abccbaandy

Thanks for the feedback, @abccbaandy.

Since changing the default would be a breaking change, we will revisit this topic for Spring Framework 6.

sbrannen avatar May 27 '21 14:05 sbrannen

I've slightly repurposed this issue for revisiting our fallback parsing behavior there. Based on the scenario described above, we should indeed leniently accept the default parse format of every java.time type.

This is different from switching useIsoFormat to true since that affects printing as well and generally bypasses other style settings. Also, this new ISO fallback behavior only kicks in if no fallback patterns have been specified, so it's easy to bypass.

jhoeller avatar Oct 18 '22 10:10 jhoeller

Related enhancements in 6.0: #1215, #19846

jhoeller avatar Oct 18 '22 10:10 jhoeller