RFR: 8317742: ISO Starndard Date Format implementation consistency on DateTimeFormatter and String.format [v2]

Shaojin Wen duke at openjdk.org
Thu Oct 12 06:04:12 UTC 2023


On Mon, 9 Oct 2023 23:21:57 GMT, Shaojin Wen <duke at openjdk.org> wrote:

>> j.u.Formatter now prints "%tF" (iso standard date) and the result is incorrect when processing year < 0 or year > 9999
>
> Shaojin Wen has updated the pull request incrementally with one additional commit since the last revision:
> 
>   No longer localize printed numbers

# Summary
Keep the behavior of String.format('%tF') when printing LocalDate consistent with the behavior of DateTimeFormatter.ISO_LOCAL_DATE.


# Problem
1. When using String.format('%tF', localDate), use get(ChronoField.YEAR_OF_ERA). When the value range is not [0,9999], the behavior is inconsistent with DateTimeFormatter.ISO_LOCAL_DATE.format.
2. When String.format('%tF', localDate), Locale will be processed, which is inconsistent with the standard.

* the year value range [-∞, -1] changes:
``` java
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
And the "%tF" format no longer handles Locale when printing numbers.



Locale.setDefault(
        Locale.forLanguageTag("th-TH-u-nu-thai"));
String.format("%tF", LocalDate.of(2001, 1, 1));
// String.format ๒๐๐๑-๐๑-๐๑
// DateTimeFormatter.ISO_LOCAL_DATE.format 2001-01-01


# Solution
1. When String.format('%tF', localDate) uses ChronoField.YEAR instead of ChronoField.YEAR_OF_ERA, when year > 9999, the prefix is added with '+'
2. And don't use Locale to print numbers.


# Specification
* [-∞, -1000]

String.format("%tF", LocalDate.of(-10000, 1, 1))
// -99999-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


#  Risk
1. When String.format('%tF') processes LocalDate/ZonedDateTime/OffsetDateTime, if the value range of year is not in [0-9999], the output will be different from before.
2. When the Locale of String.format('%tF') is "th-TH-u-nu-thai", the printed result is different from before.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/16033#issuecomment-1758959859


More information about the core-libs-dev mailing list