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

A.equals(B) != B.equals(A) when A,B are java.util.Date objects

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 5.0
    • core-libs
    • x86
    • windows_xp

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


      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      None needed

      A DESCRIPTION OF THE PROBLEM :
      A and B are both java.util.Date objects, and A.equals(B) != B.equals(A). As quoted from Object.equals.

           * <li>It is <i>symmetric</i>: for any non-null reference values
           * <code>x</code> and <code>y</code>, <code>x.equals(y)</code>
           * should return <code>true</code> if and only if
           * <code>y.equals(x)</code> returns <code>true</code>.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the JUnit Test I've included

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I expect this JUnit test to pass, as it should.
      ACTUAL -
      JUnit test failed because equals within core java classes doesn't really mean equal!!!

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.sql.Timestamp;
      import java.util.Date;

      import junit.framework.TestCase;

      public class SanityTest extends TestCase {

        public void testEqualsReallyDoesMeanEqual() throws Exception {
          Date date = new Date(-62135744400000L);
          POJO pojo = new POJO();
          pojo.setDate(new Timestamp(-62135744400000L));
          
          assertTrue(date.equals(pojo.getDate()));
          assertTrue(pojo.getDate().equals(date));
        }
        
        private class POJO {
          Date date;

          public Date getDate() {
            return date;
          }

          public void setDate(Date date) {
            this.date = date;
          }
        }
      }
      ---------- END SOURCE ----------

            Unassigned Unassigned
            peytoia Yuka Kamiya (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: