title: java.util.Date equals() doesn't seem to work as expected
body: <h2>Problem</h2>
  
  <p>I have a <code>Map&lt;Date, Foo&gt;</code>, and a list of objects from the database with an <code>effectiveDate</code> property, and I want to check to see if the <code>Date</code> keys in my map are equal to any of the <code>effectiveDate</code>s in the database - if so, do stuff with <code>Foo</code>.</p>
  
  <p>The code looks something like this:</p>
  
  <pre><code>for (Bar bar : databaseBars) {
    Foo foo = new Foo();
    if (dateMap.containsKey(bar.getEffectiveDate()) {
      foo = dateMap.get(bar.getEffectiveDate());
    }
    // do stuff with foo and bar
  }
  </code></pre>
  
  <p>However, the <code>dateMap.containsKey</code> call always returns false, even though I'm sure it's sometimes there.</p>
  
  <h2>Investigation</h2>
  
  <p>As a sanity check, I've printed out the long values of the dates, as well as the results of an <code>equals()</code> call and a <code>compareTo()</code> call:</p>
  
  <pre><code>for (Date keyDate : dateMap.keySet()) {
    if (keyDate == null) {
      continue; // make things simpler for now
    }
  
    Date effDate = bar.getEffectiveDate();
  
    String template = "keyDate: %d; effDate: %d; equals: %b; compareTo: %d\n";
  
    System.out.printf(template, keyDate.getTime(), effDate.getTime(), effDate.equals(keyDate), effDate.compareTo(keyDate));
  }
  </code></pre>
  
  <p>The results:</p>
  
  <pre><code>keyDate: 1388534400000; effDate: 1388534400000; equals: false; compareTo: 0
  keyDate: 1420070400000; effDate: 1388534400000; equals: false; compareTo: -1
  keyDate: 1388534400000; effDate: 1420070400000; equals: false; compareTo: 1
  keyDate: 1420070400000; effDate: 1420070400000; equals: false; compareTo: 0
  keyDate: 1388534400000; effDate: 1388534400000; equals: false; compareTo: 0
  keyDate: 1420070400000; effDate: 1388534400000; equals: false; compareTo: -1
  keyDate: 1388534400000; effDate: 1420070400000; equals: false; compareTo: 1
  keyDate: 1420070400000; effDate: 1420070400000; equals: false; compareTo: 0
  keyDate: 1388534400000; effDate: 1388534400000; equals: false; compareTo: 0
  keyDate: 1420070400000; effDate: 1388534400000; equals: false; compareTo: -1
  keyDate: 1388534400000; effDate: 1420070400000; equals: false; compareTo: 1
  keyDate: 1420070400000; effDate: 1420070400000; equals: false; compareTo: 0
  </code></pre>
  
  <h2>Question</h2>
  
  <p>1) Shouldn't <code>equals</code> and <code>compareTo</code> agree? (I assume the implementation of <code>java.util.Date</code> at least should try to follow the recommendation of <code>java.lang.Comparable</code>).</p>
  
  <p>2) <a href="http://docs.oracle.com/javase/6/docs/api/java/util/Date.html#equals(java.lang.Object)" rel="noreferrer">The <code>Date#equals</code> doc says this</a>:</p>
  
  <blockquote>
    <p>Thus, two Date objects are equal if and only if the getTime method returns the same long value for both.</p>
  </blockquote>
  
  <p>...Looks like the <code>getTime</code> method returns the same long value for both of these dates, yet <code>equal</code> returns false. Any ideas why this might be happening? I've searched high and low, but I haven't found anyone describing the same problem.</p>
  
  <p>P.S. I'm stuck using <code>java.util.Date</code>. Please don't just recommend JodaTime.</p>
  
  <p>P.P.S. I realize I could just change the structure of this code and probably get it working. But this should work, and I don't want to just work around it, unless it's a known issue or something. It just seems <em>wrong</em>.</p>