Question: When can the hashcode of two `new Date()` objects be the same?

For example,`new Date()` never has the same value, so will the hashcode value be different every time this code is run?

```
System.out.println(new Date().hashCode());

```

Answer: 
Date's `hashCode` uses `getTime` which returns the number of milliseconds since January 1, 1970, 00:00:00 GMT, calling it several times on fast modern machines is very likely to produce the same value. 

When I run it on my machine, it produces the following:

```
 System.out.println(new Date().hashCode()); //1328556618
 System.out.println(new Date().hashCode()); //1328556618
 System.out.println(new Date().hashCode()); //1328556618

```

And btw, calling `System.currentTimeMillis()` several times is very likely to produce same values.
According to the [Javadoc](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html#hashCode--), the `hashCode` is calculated as:

```
(int)(this.getTime()^(this.getTime() >>> 32))

```

Therefore, two dates' hash codes create two dates with the same time:

```
Date d1 = new Date();
System.out.println(d1.hashCode());
Date d2 = new Date(d1.getTime());
System.out.println(d2.hashCode());  // Same as d1.hashCode();

```

Or set the times to be the same:

```
Date d1 = new Date();
System.out.println(d1.hashCode());
Thread.sleep(1000);  // Just to make sure they're not the same time initially.
Date d2 = new Date();
System.out.println(d2.hashCode());  // Different to d1.hashCode();
d2.setTime(d1.getTime());
System.out.println(d2.hashCode());  // Same as d1.hashCode();

```