ISO Starndard Date Format implementation consistency on DateTimeFormatter and String.format

温绍锦(高铁) shaojin.wensj at alibaba-inc.com
Wed Oct 4 14:22:27 UTC 2023


j.t.DateTimeFormatter defines ISO_LOCAL_DATE, j.u.Formatter.DateTime also defines ISO_STANDARD_DATE ("%tF"), and now their behavior is different outside the range of [0,9999], We run the following code and we can see their different behaviors:
```java
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE;
int[] years = {-99999, -9999, -999, -99, -9, 0, 9, 99, 999, 1999, 2999, 9999, 99999};
for (int year : years) {
 LocalDate localDate = LocalDate.of(year, 1, 1);
 System.out.println(formatter.format(localDate) + "\t\t->\t\t" + "%tF".formatted(localDate));
}
```
* output
```
-99999-01-01 -> 100000-01-01
-9999-01-01 -> 10000-01-01
-0999-01-01 -> 1000-01-01
-0099-01-01 -> 0100-01-01
-0009-01-01 -> 0010-01-01
0000-01-01 -> 0001-01-01
0009-01-01 -> 0009-01-01
0099-01-01 -> 0099-01-01
0999-01-01 -> 0999-01-01
1999-01-01 -> 1999-01-01
2999-01-01 -> 2999-01-01
9999-01-01 -> 9999-01-01
+99999-01-01 -> 99999-01-01
```
Should we keep it consistent?
- wenshao
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20231004/038c75e5/attachment.htm>


More information about the core-libs-dev mailing list