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

compareTo in java.util.Date, java.sql.Timestamp don't follow the contract

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 5.0
    • core-libs
    • x86
    • linux

      FULL PRODUCT VERSION :
      java version "1.5.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64)
      Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode, sharing)


      ADDITIONAL OS VERSION INFORMATION :
      Linux sinaloa 2.4.19-4GB #1 Fri Sep 13 13:14:56 UTC 2002 i686 unknown


      A DESCRIPTION OF THE PROBLEM :
      The documentation of Comparable.compareTo(Comparable) specifies:
      "x.compareTo(y) must throw an exception iff y.compareTo(x) throws an exception"
      The following code demonstrates that on Java 1.5.0,
      for x instanceof java.util.Date and y instanceof java.sql.Timestamp,
      this requirement is not met.
      One practical impact of this bug is, that before 1.5 it was possible to put Dates and Timestamps into the same SortedSet, while on 1.5 it is not.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      compile and run test code below

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      expect no output
      ACTUAL -
      stack trace as below

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Exception in thread "main" java.lang.ClassCastException: java.util.Date
              at java.sql.Timestamp.compareTo(Timestamp.java:474)
              at java.sql.Timestamp.compareTo(Timestamp.java:39)
              at Test.main(Test.java:7)


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class Test {
          public static void main(String[] args) {
              Comparable x = new java.util.Date();
              Comparable y = new java.sql.Timestamp(0l);

              x.compareTo(y); // succeeds
              y.compareTo(x); // fails (succeeds in version 1.3.1_08)
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      A workaround is to specify a special Comparator that uses instanceof to reverse caller, callee and result if neccessary.
      ###@###.### 2004-12-13 22:54:41 GMT

            ahandasunw Amit Handa (Inactive)
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: