我需要比较Java中的两个日期,但是从数据库获得的日期与插入的日期不同.
@Test
public void testCompareHoraHardcore() {
String sql = "update solicitacao_viagem set "
+ "hora_retorno=?, id_veiculo=?, id_responsavel_solicitacao=?, "
+ "id_responsavel_autorizante=? where id_solicitacao_viagem=? ";
Timestamp timestamp1 = new Timestamp(System.currentTimeMillis());
try {
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setTimestamp(1, timestamp1);
stmt.setInt(2, 90);
stmt.setInt(3, 337);
stmt.setInt(4, 337);
stmt.setInt(5, 91);
stmt.executeUpdate();
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
Timestamp timestamp2 = null;
try {
sql = "select * from solicitacao_viagem where id_solicitacao_viagem=?";
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setInt(1, 91);
stmt.executeQuery();
ResultSet rs = stmt.executeQuery();
rs.next();
timestamp2 = rs.getTimestamp("hora_retorno");
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
assertEquals(timestamp1.getTime(), timestamp2.getTime());
}
此测试返回:
Testcase: testCompareHoraHardcore(com.model.dao.SolicitacaoViagemDAOTest): FAILED
expected:<1360553289573> but was:<1360553289000>
junit.framework.AssertionFailedError: expected:<1360553289573> but was:<1360553289000>
at com.model.dao.SolicitacaoViagemDAOTest.testCompareHoraHardcore(SolicitacaoViagemDAOTest.java:349)
为什么MySQL正在削减最后三个数字?
我正在使用MySQL 5.5.22和Java(TM)SE运行时环境(版本1.7.0_11-b21).
列“hora_retorno”是TIMESTAMP.
提前致谢.
本文探讨了在Java中更新并读取MySQL数据库中的TIMESTAMP字段时遇到的时间戳不一致问题。具体表现为写入时间和读取时间的毫秒部分存在差异,尤其是在最后三位数上。

被折叠的 条评论
为什么被折叠?



