Skip to content
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

Document options for handling Date/Time parsing and formatting issues with JDK 20+ #33151

Closed
sbrannen opened this issue Jul 5, 2024 · 1 comment
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: documentation A documentation task
Milestone

Comments

@sbrannen
Copy link
Member

sbrannen commented Jul 5, 2024

Overview

JDK 20 adopted Unicode CLDR-14032 (Common Locale Data Repository) which changed the space character that precedes the period (AM or PM) in formatted date/time text from a standard space (" ") to a narrow non-breaking space (NNBSP: "\u202F"). Consequently, applications that rely on date/time parsing and formatting may encounter incompatible changes in behavior when using Spring on Java 20 or higher -- for example, web applications that make use of @DateTimeFormat.

On JDK 20, 21, and 22, applications can use the -Djava.locale.providers=COMPAT command-line argument for java in order to force the use of legacy locale data which uses a standard space for the space character that precedes the period in formatted date/time text.

Support for the aforementioned COMPAT mode has been removed in JDK 23; however, the Java team has introduced support for lenient parsing of space characters in JDK 23. This applies to SimpleDateFormat as well as DateTimeFormatter.

SimpleDateFormat and DateTimeFormatter Configuration

SimpleDateFormat is lenient by default; however, DateTimeFormatter instances are not lenient by default, and factory methods like DateTimeFormatter.ofLocalizedTime(...) do not create lenient formatters.

To create a lenient DateTimeFormatter, one must forgo the use of the static factory methods in DateTimeFormatter and instead make use of the DateTimeFormatterBuilder. The following example shows how to create a static factory method for a lenient DateTimeFormatter that is comparable to what DateTimeFormatter.ofLocalizedDateTime(FormatStyle, FormatStyle) produces.

pubic static DateTimeFormatter createLenientDateTimeFormatter(
         FormatStyle dateStyle, FormatStyle timeStyle) {

   return new DateTimeFormatterBuilder()
         .parseLenient()
         .appendLocalized(dateStyle, timeStyle)
         .toFormatter()
         .withChronology(IsoChronology.INSTANCE);
}

Proposal

In Spring Framework, we do not plan to introduce additional support for lenient parsing of dates using AM/PM in conjunction with @DateTimeFormat. We also do not plan to introduce additional support to control the format used when rendering/printing String representations of dates or times.

Rather, we will document the options that Spring provides to assist developers who encounter date/time parsing and formatting issues, and we will refer developers to the updated documentation in JEP 252 provided by the JDK team.

For example, @DateTimeFormat already provides support for fallback patterns that allow for lenient parsing of date/time inputs.

Related Resources

Related Issues

@sbrannen sbrannen added type: documentation A documentation task in: core Issues in core modules (aop, beans, core, context, expression) labels Jul 5, 2024
@sbrannen sbrannen added this to the 6.1.11 milestone Jul 5, 2024
@sbrannen sbrannen self-assigned this Jul 5, 2024
@sbrannen sbrannen modified the milestones: 6.1.11, 6.1.x, 6.1.12 Jul 10, 2024
@sbrannen sbrannen modified the milestones: 6.1.12, 6.1.x, 6.1.13 Aug 13, 2024
@sbrannen sbrannen modified the milestones: 6.1.13, 6.1.14 Sep 11, 2024
@sbrannen sbrannen modified the milestones: 6.1.14, 6.1.15 Oct 16, 2024
@sbrannen sbrannen changed the title Document potential Date/Time parsing issues with JDK 20+ Document options for handling Date/Time parsing issues with JDK 20+ Oct 19, 2024
sbrannen added a commit that referenced this issue Oct 20, 2024
This commit updates our Date/Time formatting/printing tests to
demonstrate that the use of fallback patterns can help mitigate
locale-based parsing/formatting issues beginning with JDK 20.

The documentation within the tests is intentionally rather thorough for
two reasons:

1. We need to understand exactly what it is we are testing and why the
   tests are written that way.

2. We may re-use parts of the documentation and examples in forthcoming
   documentation that we will provide to users.

See gh-33151
@sbrannen sbrannen changed the title Document options for handling Date/Time parsing issues with JDK 20+ Document options for handling Date/Time parsing and formatting issues with JDK 20+ Oct 20, 2024
@sbrannen
Copy link
Member Author

Rather, we will document the options that Spring provides to assist developers who encounter date/time parsing and formatting issues, and we will refer developers to the updated documentation in JEP 252 provided by the JDK team.

Javadoc and the reference guide have been updated in bfde33a, and a new Date and Time Formatting with JDK 20 and higher wiki page has been introduced as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: documentation A documentation task
Projects
None yet
Development

No branches or pull requests

1 participant