Skip to content

fix: accept NNBSP before AM/PM in default Date parsing - #3091

Open
arimu1 wants to merge 1 commit into
google:mainfrom
arimu1:fix/2689-date-nnbsp-parse
Open

fix: accept NNBSP before AM/PM in default Date parsing#3091
arimu1 wants to merge 1 commit into
google:mainfrom
arimu1:fix/2689-date-nnbsp-parse

Conversation

@arimu1

@arimu1 arimu1 commented Aug 7, 2026

Copy link
Copy Markdown

Purpose

Fixes #2689 — default Date deserialization fails when the date string uses a different space character before AM/PM than the current JDK's DateFormat expects.

Starting with the CLDR 42 update in JDK 21 (JDK-8284840), DateFormat.getDateTimeInstance for some locales emits U+202F (narrow no-break space) before AM/PM instead of U+0020. Dates written on one JDK version therefore often fail to parse on another when using Gson's default Date adapter (DefaultDateTypeAdapter, formerly DateTypeAdapter).

Description

In DefaultDateTypeAdapter.deserializeToDate, if DateFormat.parse fails for the original string, retry with alternate space variants before the AM/PM marker:

  • normalize U+202F / U+00A0 to a regular space
  • replace the regular space immediately before AM/PM with U+202F

This keeps default Date adapters interoperable across JDK 8–21+ without changing the serialization format (still whatever the running JDK's DateFormat produces). ISO-8601 fallback is unchanged; the failure was never in ISO8601Utils itself but in the DateFormat attempts that precede it.

Related discussion: #2450 (test fragility around the same space character), #2472 (possible future default ISO-8601 date format).

Checklist

  • New code follows the Google Java Style Guide
    Checked with mvn -pl gson spotless:check.
  • If necessary, new public API validates arguments, for example rejects null
    N/A — no new public API.
  • New public API has Javadoc
    N/A
    • Javadoc uses @since $next-version$
      N/A
  • If necessary, new unit tests have been added
    • Assertions in unit tests use Truth, see existing tests
    • No JUnit 3 features are used (such as extending class TestCase)
    • If this pull request fixes a bug, a new test was added for a situation which failed previously and is now fixed
      DefaultDateTypeAdapterTest#testParsingAcceptsNarrowNoBreakSpaceBeforeAmPm covers regular space, U+202F, and U+00A0 for AM and PM.
  • mvn clean verify javadoc:jar passes without errors
    Focused: mvn -pl gson -Dtest=DefaultDateTypeAdapterTest test12/12 (Temurin 21). Also manually verified issue JDK21 toJson failure #2689 payloads parse on JDK 21 and JDK 17 after the fix.

JDK 21+ CLDR uses U+202F (narrow no-break space) before AM/PM in
DateFormat output, while older JDKs use a regular space. Default
Date deserialization now retries with both space variants so dates
remain interoperable across JDK versions.

Fixes google#2689
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JDK21 toJson failure

1 participant