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

java.sql.Date.valueOf(LocalDate) does not handle 5-digit years correctly

XMLWordPrintable

    • generic
    • generic

      FULL PRODUCT VERSION :
      java version "1.8.0_144"
      Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
      Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Darwin 16.7.0 Darwin Kernel Version 16.7.0: Thu Jun 15 17:36:27 PDT 2017; root:xnu-3789.70.16~2/RELEASE_X86_64

      A DESCRIPTION OF THE PROBLEM :
      Calling toString() on java.sql.Date and java.time.LocalDate that contain 5-digit years result in invalid output. java.time.LocalDate prepends the year with a '+', and java.sql.Date replaces the first character of the year with a NUL character.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Execute the following commands:

      $ cat <<EOF > FiveDigitYear.java
      import java.sql.Date;
      import java.time.LocalDate;

      class FiveDigitYear {
        public static void main(String[] args) {
          LocalDate fiveDigit = LocalDate.of(10000, 1, 1);
          System.out.println("java.time.LocalDate: " + fiveDigit.toString());
          System.out.println("java.sql.Date: " + Date.valueOf(fiveDigit).toString());
        }
      }
      EOF

      $ javac FiveDigitYear.java
      $ java FiveDigitYear

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      $ java FiveDigitYear
      java.time.LocalDate: 10000-01-01
      java.sql.Date: 10000-01-01
      $ java FiveDigitYear | hexdump -C
      00000000 6a 61 76 61 2e 74 69 6d 65 2e 4c 6f 63 61 6c 44 |java.time.LocalD|
      00000010 61 74 65 3a 20 31 30 30 30 30 2d 30 31 2d 30 31 |ate: 10000-01-01|
      00000020 0a 6a 61 76 61 2e 73 71 6c 2e 44 61 74 65 3a 20 |.java.sql.Date: |
      00000030 31 30 30 30 30 2d 30 31 2d 30 31 0a |10000-01-01.|
      0000003c
      ACTUAL -
      $ java FiveDigitYear
      java.time.LocalDate: +10000-01-01
      java.sql.Date: 000-01-01
      $ java FiveDigitYear | hexdump -C
      00000000 6a 61 76 61 2e 74 69 6d 65 2e 4c 6f 63 61 6c 44 |java.time.LocalD|
      00000010 61 74 65 3a 20 2b 31 30 30 30 30 2d 30 31 2d 30 |ate: +10000-01-0|
      00000020 31 0a 6a 61 76 61 2e 73 71 6c 2e 44 61 74 65 3a |1.java.sql.Date:|
      00000030 20 00 30 30 30 2d 30 31 2d 30 31 0a | .000-01-01.|
      0000003c

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.sql.Date;
      import java.time.LocalDate;

      class FiveDigitYear {
        public static void main(String[] args) {
          LocalDate fiveDigit = LocalDate.of(10000, 1, 1);
          System.out.println("java.time.LocalDate: " + fiveDigit.toString());
          System.out.println("java.sql.Date: " + Date.valueOf(fiveDigit).toString());
        }
      }
      ---------- END SOURCE ----------

            lancea Lance Andersen
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: