Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8317752

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

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Withdrawn
    • Icon: P4 P4
    • 22
    • core-libs
    • None
    • behavioral
    • minimal
    • Hide
      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. Negative years will not be prefixed with a "-" sign, and positive years > 9999, will be prefixed with a "+" sign as per ISO 8601.
      Show
      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. Negative years will not be prefixed with a "-" sign, and positive years > 9999, will be prefixed with a "+" sign as per ISO 8601.
    • Java API
    • SE

      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.

            rriggs Roger Riggs
            rriggs Roger Riggs
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: