java.sql.Timestamp是从java.util.Date创建的,为什么总是在()之前呢? [英] java.sql.Timestamp created from java.util.Date, why always before() it?

查看:246
本文介绍了java.sql.Timestamp是从java.util.Date创建的,为什么总是在()之前呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在发现错误之后,我注意到如果我从java.util.Date创建一个java.sql.Timestamp,使用占用毫秒的构造函数,则Date实例总是在()时间戳之后。这是令人费解的,因为(a)before()的合同指定了严格的比较,(b)如果不相等,Timestamp,因为它有纳秒,可能本身是在Date之后。但结果是相反且可重复的(使用JDK 1.6和1.7,具有不同的JVM时区)。比较两个Dates可以正常工作,但在Date上调用before()或after()并给出Timestamp参数会产生意外结果。

Following a bug, I've noticed that if I create a java.sql.Timestamp from a java.util.Date, using the constructor that takes the milliseconds, the Date instance is always after() the Timestamp. This is puzzling, since (a) the contract for before() specifies a strict comparison and (b) if not equal, the Timestamp, because it has nanoseconds, might itself be after() the Date. But the results are opposite and repeatable (with JDK 1.6 and 1.7, with different JVM timezones). Comparing two Dates works correctly, but calling before() or after() on a Date and giving a Timestamp argument has unexpected results.

下面的示例代码有两个Date和一个Timestamp实例,所有这些实例都具有相同的毫秒值。然而,将Date与时间戳进行比较会显示Date之后()时间戳。

The sample code below has two Date and one Timestamp instance, all of them with the same millisecond value. Yet comparing a Date with a Timestamp shows the Date to be after() the Timestamp.

import java.util.Date;
import java.sql.Timestamp;

public class X extends Date {

    public static void main(String[] args) {
        Date d1 = new Date();
        Date d2 = new Date(d1.getTime());
        Timestamp t = new Timestamp (d1.getTime());
        System.out.println ("date1 = " + d1 + " (" + d1.getTime() + ")" );
        System.out.println ("date2 = " + d2 + " (" + d2.getTime() + ")" );
        System.out.println ("timestamp = " + t + "  (" + t.getTime() + ")" );
        System.out.println ("d1 before d2: " + d1.before(d2));
        System.out.println ("d1 after  d2: " + d1.after(d2));
        System.out.println ("d1 before ts: " + d1.before(t));
        System.out.println ("d1 after  ts: " + d1.after(t)); //why true?
    }
}

样本输出:

C:\>\Java\jdk1.7.0_05\bin\java X
date1 = Tue Oct 30 10:15:59 EDT 2012 (1351606559812)
date2 = Tue Oct 30 10:15:59 EDT 2012 (1351606559812)
timestamp = 2012-10-30 10:15:59.812  (1351606559812)
d1 before d2: false
d1 after  d2: false
d1 before ts: false
d1 after  ts: true

最后一行是好奇的。

谢谢。

推荐答案

如果看一下内部表示以及之后()方法的比较,你会看到例如

If you look at the internal representation and what is compared in the after() method, you see that for example for

millis = 1351607849957

你得到日期 with

fastTime = 1351607849957

时间戳

fastTime = 1351607849000
nanos = 957000000

因为所有比较的是 fastTime 部分,您可以获得观察到的行为。
正如@ user714965指出的那样,你不应该将时间戳视为日期

Since all that is compared is the fastTime part, you get your observed behaviour. As @user714965 points out above, you aren't supposed to treat a Timestamp as a Date.

这篇关于java.sql.Timestamp是从java.util.Date创建的,为什么总是在()之前呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆