Add java.time Instant, Year, YearMonth and MonthDay support to DateTimeConverter - #2221
Open
BalusC wants to merge 1 commit into
Open
Add java.time Instant, Year, YearMonth and MonthDay support to DateTimeConverter#2221BalusC wants to merge 1 commit into
BalusC wants to merge 1 commit into
Conversation
…meConverter The type attribute now also accepts "instant", "year", "yearMonth" and "monthDay". None of these types can be formatted or parsed with a localized date/time style, so dateStyle and timeStyle are ignored for them and the ISO 8601 form of the type is used when no pattern is specified: ISO_INSTANT for instant, "uuuu" for year, "uuuu-MM" for yearMonth and "--MM-dd" for monthDay. This follows the existing offsetTime/offsetDateTime/zonedDateTime types, which also use a fixed ISO formatter. An Instant carries no zone, so for that type the timeZone property is passed to the formatter, which makes a pattern containing date or time fields resolvable. The new types are also added to the message selection in getAsObject, which would otherwise swallow the parse exception and silently convert bad input to null. Closes #2211 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
typeattribute ofDateTimeConverter/<f:convertDateTime>now also accepts"instant","year","yearMonth"and"monthDay", corresponding tojava.time.Instant,java.time.Year,java.time.YearMonthandjava.time.MonthDay.Default format
None of these four types can be formatted or parsed with a localized date/time style —
DateTimeFormatter.ofLocalizedDate()throwsUnsupportedTemporalTypeExceptionfor all of them (missingDayOfMonth, orYearOfEraforMonthDay), andjava.timeexposes no API for the CLDR available-formats (yMM,Md) that would be needed to derive a localized year-month or month-day pattern. HencedateStyleandtimeStyleare ignored for these types and, when nopatternis specified, the ISO 8601 form of the type is used:instantDateTimeFormatter.ISO_INSTANTInstant.toString()/Instant.parse()yearuuuuYear.toString()/Year.parse()yearMonthuuuu-MMYearMonth.toString()/YearMonth.parse()monthDay--MM-ddMonthDay.toString()/MonthDay.parse()This follows the existing
offsetTime/offsetDateTime/zonedDateTimetypes, which also use a fixed ISO formatter and ignore the styles. Parsing stays strict, as the spec requires:ResolverStyle.STRICTwithIsoChronology, so2026-13and--02-30are rejected while--02-29is accepted.Time zone
An
Instantcarries no zone, so for the typeinstantthetimeZoneproperty is passed to the underlyingDateTimeFormatter. Without it apatterncontaining date or time fields cannot be resolved and formatting blows up.ISO_INSTANTitself always renders in UTC regardless, which is documented.Message selection
The four new types are added to the message selection in
getAsObject(). A type absent from that switch falls through thecatchwithout throwing, which would have silently converted unparseable input tonull.Not included
java.time.Durationandjava.time.Periodfrom the issue are deliberately left out: they areTemporalAmount, notTemporalAccessor, soDateTimeFormattercannot handle them at all. They warrant their own converter, to be discussed separately.Tests
DateTimeConverterTest: ISO round-trip per type, explicit patterns,instantwith a pattern inEurope/Amsterdam, leap day, andConverterExceptionon unparseable input. 153 tests green in theapimodule.Spec2211IT+spec2211.xhtml+Spec2211Beanundertck/faces50/converters, covering the same through<f:convertDateTime>. Compiles; not yet run against a container.Closes #2211
🤖 Generated with Claude Code (Opus 5)