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

Timestamp.equals()-method breaks symmetrie-contract of Object.equals(Object o)

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4
    • Resolution: Not an Issue
    • Affects Version/s: 6u26
    • Fix Version/s: None
    • Component/s: core-libs
    • Labels:
    • Subcomponent:
    • CPU:
      x86
    • OS:
      windows_xp

      Description

      FULL PRODUCT VERSION :
      1.6.0_24

      ADDITIONAL OS VERSION INFORMATION :
      5.1.2600

      A DESCRIPTION OF THE PROBLEM :
      The equals-method in class Timestamp (for non-Timestamp objects) is defined as follows:
          public boolean equals(java.lang.Object ts) {
            if (ts instanceof Timestamp) {
      return this.equals((Timestamp)ts);
            } else {
      return false;
            }
          }
      Therefore, all calls to this method with java.util.Date objects will always return false due to the instanceof check. On the other hand, calls to a java.util.Date object with a Timestamp as a parameter will result in a check, whether or not both objects point to the same time.

      This Bug has been reported three times already:
      4170629 (that was 1998!)
      6587778 (marked as duplicate of 4170629)
      6609514 (marked as duplicate of 4170629)

      In my opinion the bug was misunderstood in 4170629 and therefore wrongfully closed.

      This bug is rather severe, as persistence-frameworks like hibernate might return Timestamp instead of Date objects. Comparisons on these objects with Date objects in the main code then might lead to undesired and unexpected results.

      Please do not mark this bug as duplicate of 4170629 without reopening this bug report!

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Create a Date/Timestamp objectpair, with the same underlying millisecond timespan. Compare both objects vice versa. Date.equals(Timestamp) gives true as a result, Timestamp.equals(Date) gives false.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Expected result is, that both comparisons return true as a result.
      ACTUAL -
      A comparison Date.equals(Timestamp) results in true, the comparison Timestamp.equals(Date) results in false.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      java.util.GregorianCalendar cal = new java.util.GregorianCalendar(2011, 5, 28);
      java.sql.Timestamp ts = new java.sql.Timestamp(cal.getTimeInMillis());
      java.util.Date date = new java.util.Date(cal.getTimeInMillis());
      System.out.println("Date.equals(TimeStamp): " + date.equals(ts));
      System.out.println("Timestamp.equals(Date): " + ts.equals(date));
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      As long as this bug is not fixed, the only solution seems to be no longer to use equals in dates but insted use Date.compareTo(Date) == 0.

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: