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.
You can generally expect two unequal objects to have different hashcodes. But according to the [contract](https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#hashCode()), there is never any *guarantee* that hashcodes for two objects will be different:

> 
> * It is not required that if two objects are unequal according to the [`equals(java.lang.Object)`](https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#equals(java.lang.Object)) method, then calling the hashCode method on each of the two objects must produce distinct integer results.
> 
> 
> 

But your statement that every new `Date` object has a different value is not correct. `Date` only has millisecond precision, which means that you can generate many objects in a row with the same value.