数据库中的保留日期不等于检索日期 [英] Persisted date in database not equal to retrieved date
本文介绍了数据库中的保留日期不等于检索日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个具有Date属性的简单实体类.此属性对应于MySQL datetime列.
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
所以两个日期之间有一个很小的差距.我真的很想知道这种差距可能从何而来.它并不总是相同的,并且每次我开始测试过程时都会有所不同. 在我的数据库中,存储的日期是2012-10-13 22:15:38.0.
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?
推荐答案
查看全文