I have a simple entity class that has a Date property. This property corresponds to a MySQL datetime column.
@Entity
public class Entity {
@Column(name = "start_date")
@Temporal(TemporalType.TIMESTAMP)
private java.util.Date startDate;
}
This is what an integration test I wrote looks like:
java.util.Date now = new java.util.Date();
Entity entity = new Entity();
entity.setStartDate(now);
entityService.save(entity); // save entity to database
entity = entityService.get(entity.getId()); // get entity back from database
Assert.assertEquals(entity.getStartDate(), now);
I would expect those two dates to be equal but they are not! Actually, I have :
now.getTime() = 1350160029831
entity.getStartDate().getTime() = 1350160029000
so there is a small gap between the two dates. I am really wondering where this gap could come from. It is not always the same and varies each time I start the testing process.
In my database, the date stored is 2012-10-13 22:15:38.0.
Do I really need to clear milliseconds somewhere?
From the MySQL documentation:
Note that you shouldn’t use equals to compare dates anyway, because the various Date subclasses have buggy implementations, which cause problems such as
a.equals(b) && !b.equals(a):