79652476

Date: 2025-06-04 09:29:19
Score: 1
Natty:
Report link

As a supplement to the correct and helpful answer by Sweeper this answer digs a bit deeper. You asked why your parsing threw an exception, and Sweeper already correctly said that it’s because yyyy denotes a variable-width field. I want to show you the two places in the documentation where this is specified. Since for example MM gives a fixed-width field of exactly two digits, one can easily get surprised when neither yyyy nor uuuu gives a fixed-width 4-digit field.

The documentation of DateTimeFormatterBuilder.appendPattern() first refers to DateTimeFormatter for a user-focused description of the patterns. It in turn says specifically about years:

The count of letters determines the minimum field width below which padding is used. … If the count of letters is less than four … Otherwise, the sign is output if the pad width is exceeded, as per SignStyle.EXCEEDS_PAD.

So this allows yyyy to print, and as a consequence also parse a year with either 4 digits or more than four digits with a sign.

The documentation of DateTimeFormatterBuilder.appendPattern() goes on to specify that appending a pattern of four or more letters y is equivalent to appendValue(ChronoField.YEAR_OF_ERA, n, 19, SignStyle.EXCEEDS_PAD) where n is the count of letters. We see that yyyy allows a field of width 4 through 19.

Links

Reasons:
  • RegEx Blacklisted phrase (1): I want
  • Long answer (-1):
  • Has code block (-0.5):
  • Unregistered user (0.5):
  • Low reputation (1):
Posted by: Hamza Schmit