-
Sub-task
-
Resolution: Delivered
-
P4
-
23
-
generic
-
generic
Parsing of date/time strings now allows the "loose matching" of spaces. This enhancement is mainly to address the [incompatible changes](https://www.oracle.com/java/technologies/javase/20-relnote-issues.html#JDK-8284840) introduced in JDK 20 with CLDR version 42. That version replaced ASCII spaces (`U+0020`) between time and the am/pm marker with `NNBSP` (Narrow No-Break Space, `U+202F`) in some locales. The "loose matching" is performed in the "lenient" parsing style for both date/time parsers in `java.time.format` and `java.text` packages. In the "strict" parsing style, those spaces are considered distinct, as before.
To utilize the "loose matching" in the `java.time.format` package, applications will need to explicitly set the leniency by calling `DateTimeFormatterBuilder.parseLenient()` because the default parsing mode is strict:
```
var dtf = new DateTimeFormatterBuilder()
.parseLenient()
.append(DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT))
.toFormatter(Locale.ENGLISH);
```
In the `java.text` package, the default parsing mode is lenient. Applications will be able to parse all space separators automatically, which is the default behavior changes with this feature. In case they need to strictly parse the text, they can do:
```
var df = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.ENGLISH);
df.setLenient(false);
```
To utilize the "loose matching" in the `java.time.format` package, applications will need to explicitly set the leniency by calling `DateTimeFormatterBuilder.parseLenient()` because the default parsing mode is strict:
```
var dtf = new DateTimeFormatterBuilder()
.parseLenient()
.append(DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT))
.toFormatter(Locale.ENGLISH);
```
In the `java.text` package, the default parsing mode is lenient. Applications will be able to parse all space separators automatically, which is the default behavior changes with this feature. In case they need to strictly parse the text, they can do:
```
var df = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.ENGLISH);
df.setLenient(false);
```