Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
比较Java中的两个日期,一个来自MySQL_Java_Mysql_Timestamp - Fatal编程技术网

比较Java中的两个日期,一个来自MySQL

比较Java中的两个日期,一个来自MySQL,java,mysql,timestamp,Java,Mysql,Timestamp,我需要比较Java中的两个日期,但从数据库中获取的日期与插入的日期值不同 @Test public void testCompareHoraHardcore() { String sql = "update solicitacao_viagem set " + "hora_retorno=?, id_veiculo=?, id_responsavel_solicitacao=?, " + "id_responsavel_autorizant

我需要比较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)
Testcase:TestCompareHolaHardcore(com.model.dao.requestacaoviagedaotest):失败
预期:但是:
junit.framework.AssertionFailedError:应为:但为:
在com.model.dao.requestacoaviemagedaotest.testCompareHoraHardcore上(requestacoaviemagedaotest.java:349)
为什么MySQL要削减最后三个数字

我使用的是MySQL 5.5.22和Java(TM)SE运行时环境(build 1.7.0_11-b21)

“hora_Retrono”列是时间戳


提前感谢。

根据这一点,MySQL不在时间戳列(或其他日期时间类型)中存储小数秒

MySql将
DATETIME
TIMESTAMP
存储到seconds精度。这里有一段引用自


您必须检查文档,但可能是MySQL以秒为单位存储时间戳,而不是以毫秒为单位…?Thank=d。但我如何存储数据库中的e retrive DateTime值以在Java中进行比较?使用Java Date或将列更改为DATETIME,或两个?您可以将毫秒值四舍五入到最接近的千。
A DATETIME or TIMESTAMP value can include a trailing fractional seconds
part in up to microseconds(6 digits) precision. 

Although this fractional part is recognized, 
it is discarded from values stored into DATETIME or TIMESTAMP columns