Summary
Correct the behavior of String.format('%tF') when printing LocalDate to be consistent with the behavior of DateTimeFormatter.ISO_LOCAL_DATE.
Problem
When using String.format('%tF', localDate), get(ChronoField.YEAR_OF_ERA) is used, year-of-era is only valid in the range zero to Integer.MAX_VALUE. When the year value range is outside of the range [0,9999], the behavior is inconsistent with DateTimeFormatter.ISO_LOCAL_DATE.format.
The year value range [-∞, -1] changes:
String.format("%tF", LocalDate.of(-10000, 1, 1))
// String.format 10001-01-01
// DateTimeFormatter -99999-01-01
The year value range [10000, +∞] changes:
String.format("%tF", LocalDate.of(10000, 1, 1)):
// String.format 10000-01-01
// DateTimeFormatter.format +10000-01-01
Solution
ChronoField.YEAR should be used instead of ChronoField.YEAR_OF_ERA when formatting String.format('%tF', localDate). Negative years will be prefixed with "-" and when the year > 9999, the prefix is added with '+' as per ISO 8601.
[-∞, -1000]
String.format("%tF", LocalDate.of(-10000, 1, 1))
// -10000-01-01
The year value range [-999, -1], padded to 4 digits
String.format("%tF", LocalDate.of(-1, 1, 1))
// -0001-01-01
The year value range [1, 9999], padded to 4 digits
String.format("%tF", LocalDate.of(1, 1, 1))
// 0001-01-01
The year value range [10000, -∞], prefix prints '+'
String.format("%tF", LocalDate.of(10000, 1, 1))
// +10000-01-01
Specification
The results are corrected to match DateTimeFormatter.ISO_LOCAL_DATE as per the current specification.
- csr of
-
JDK-8317742 ISO Standard Date Format implementation consistency on DateTimeFormatter and String.format
-
- Resolved
-