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

java.sql.Date.valueOf accepts invalid dates

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P5 P5
    • None
    • 6u22
    • core-libs
    • None
    • generic
    • generic

      Test with 6u22:

      % cat MyTest.java
      public class MyTest {
          public static void main(String[] args) {
              java.sql.Date jsd1 = java.sql.Date.valueOf("0000-01-01");
              java.sql.Date jsd2 = java.sql.Date.valueOf("0001-01-01");
              System.out.println(jsd1);
              System.out.println(jsd2);
              System.out.println(jsd1.equals(jsd2));
              System.out.println(jsd1.getTime());
              System.out.println(jsd2.getTime());
          }
      }

      % javac MyTest.java
      % java MyTest
      0001-01-01
      0001-01-01
      false
      -62167395600000
      -62135773200000

      The internal representation of the two sql.Dates differ by 366 days in this case and therefore the equals method return false. The toString method prints 0001-01-01 in both cases however.

      java.sql.Date.valueOf accepts invalid dates, because year 0 does not exist (the transition from BC to AD is ..., 2 BC, 1 BC, 1 AD, 2 AD).

            lancea Lance Andersen
            jloefflm Johann Löfflmann (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: