Skip to content

feat: add ZonedDateTime converters (#1017) - #1294

Open
Mikkey-f wants to merge 8 commits into
apache:mainfrom
Mikkey-f:feat/1020-zoneddatetime-converters
Open

Mikkey-f wants to merge 8 commits into
apache:mainfrom
Mikkey-f:feat/1020-zoneddatetime-converters

Conversation

@Mikkey-f

@Mikkey-f Mikkey-f commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Purpose of the pull request

Related: #1017 (java.time.ZonedDateTime slice).

java.time.ZonedDateTime is on the #1017 list of missing converters. Without it, a ZonedDateTime field fails on read and write with Can not find 'Converter' support class ZonedDateTime, while the sibling date-time types (LocalDateTime, OffsetDateTime, Instant) are supported. This adds the STRING/NUMBER/DATE converters for it.

Supersedes #1020. The initial implementation and tests are @alexsmolya's work from that PR (the first commit keeps his authorship). This branch rebases that work onto current main, moves the tests to the converters/zoneddatetime package used by the other families, and addresses the review feedback on #1020.

What's changed?

  • New converters/zoneddatetime package, registered in DefaultConverterLoader:
    • ZonedDateTimeStringConverter (read + write): uses ISO_ZONED_DATE_TIME when no pattern is configured, otherwise the @DateTimeFormat pattern through the shared bounded formatter cache in DateUtils. Reads are strict: text without a zone is rejected instead of falling back to the server zone.
    • ZonedDateTimeNumberConverter (read + write): reads the Excel serial via DateUtils.getLocalDateTime(..., isDate1904(...)) and returns null for invalid serials (previously an NPE); writes DateUtil.getExcelDate.
    • ZonedDateTimeDateConverter (write only, DATE cell): normalizes an empty @DateTimeFormat to null, so the default yyyy-MM-dd HH:mm:ss format applies instead of writing a format-less DATE cell.
  • DateUtils: adds parseZonedDateTime(String, String, Locale) and format(ZonedDateTime, String, Locale) that use the existing cache (getCacheDateTimeFormat stays private).
  • Registration mirrors the existing families; no behavior changes for other types.

Verification

  • Red side:
    • removing the null guard in ZonedDateTimeNumberConverter → numberConverterReturnsNullForInvalidExcelDate fails with an NPE (LocalDateTime.atZone);
    • removing the registrations → all three round-trip cases fail with Can not find 'Converter' support class ZonedDateTime.
  • Green side: ZonedDateTimeConverterTest 10/10, ZonedDateTimeRoundTripTest 3/3 (XLSX, XLS, CSV), DateUtilsTest +1.
  • Full build mvn clean package -B -Dmaven.test.skip=false -pl fesod-common,fesod-shaded,fesod-sheet: 1,862 tests, 0 failures.
  • spotless:check and javadoc:javadoc pass.

Notes

  • DATE and NUMBER representations intentionally drop the zone and keep the local date-time (Excel date semantics); STRING keeps the zone text.
  • Tests use a non-UTC zone (Asia/Shanghai) and clear the shared formatter cache after each test, per the latest review on feat: add ZonedDateTime converters #1020.

Checklist

  • I have read the Contributor Guide.
  • I have written the necessary doc or comment.
  • I have added the necessary unit tests and all cases have passed.

@Mikkey-f

Copy link
Copy Markdown
Contributor Author

Added one commit to apply the same CSV fallback to ZonedDateTime (related to #1334):

  • ZonedDateTimeStringConverter now retries text without a zone as a local date-time and attaches ZoneId.systemDefault() when no @DateTimeFormat is configured. Previously, CSV output from ZonedDateTimeDateConverter (yyyy-MM-dd HH:mm:ss) could not be read back.
  • Added a javadoc warning on ZonedDateTimeDateConverter and a round-trip test covering XLSX, XLS and CSV with an explicit DATE converter on write and a plain field on read.
  • Explicit @DateTimeFormat patterns keep the existing strict behavior.

Verification: targeted 11 + 3 passed; full suite 60 + 1,806, 0 failures; spotless and javadoc pass.

Revert the string-converter system-zone fallback and write ISO zoned text for CSV instead, keeping strict parsing as requested in apache#1020.
@Mikkey-f

Copy link
Copy Markdown
Contributor Author

Correction to my previous comment: I reverted the string-converter fallback and fixed the CSV path on the write side instead.

ZonedDateTimeDateConverter now writes an ISO-8601 string with the zone (ISO_ZONED_DATE_TIME) for CSV when no explicit @DateTimeFormat is configured. This keeps ZonedDateTimeStringConverter strict, as requested in the earlier review on #1020 (no ZoneId.systemDefault() fallback), while making the CSV value readable back with the zone preserved. XLSX/XLS DATE cells still store only the local date-time and discard the zone; the javadoc documents that limitation.

The previous fallback commit is still in the branch history but is reverted by the new commit; the final tree keeps parsing strict.

The new commit also adds round-trip coverage: CSV preserves the zone, XLSX/XLS read back the local date-time with the JVM default zone.

Verification: targeted ZonedDateTimeConverterTest + ZonedDateTimeDateRoundTripTest: 11 + 3 passed; reverting the write-side change reproduces the CSV failure; full suite 60 + 1,806, 0 failures; spotless and javadoc pass.

…me-converters

# Conflicts:
#	fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/DefaultConverterLoader.java

This branch has not been deployed

No deployments
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.

2 participants