English 中文(简体)
java.util.Timestamp.after() 毫秒比较错误?
原标题:java.util.Timestamp.after() wrong when comparing milliseconds?

我从Oracle数据库中提取日期。它们设置在java.util.Date字段上,实际上是java.sql.Timestamp实例(它是Java.util.Date的子类)。如果我通过调用第一个日期的after()比较来自两个不同数据库记录的两个时间戳,并将其与第二个进行比较,则当日期的所有部分相同时,得到错误答案,除了毫秒。

以下所有应该结果为“true”,但第二组数字不是:

firstDate  = 1/1/2000 12:00:20:00
secondDate = 1/1/2000 12:00:10:00
result = firstDate.after(secondDate);
result is TRUE <-- EXPECTED RESULT

firstDate  = 1/1/2000 12:00:00:10
secondDate = 1/1/2000 12:00:00:00
result = firstDate.after(secondDate);
result is FALSE <-- NOT EXPECTED, result should be TRUE 

我知道,南索岛与安普什班的日子分开存放,如果是问题,我会很奇怪。

最佳回答

你可以将它们进行比较,但只能通过比较毫秒数来进行。虽然这很丑陋,但它似乎在所有情况下都起作用(不论是 java.sql.Timestamp 还是 java.util.Date)。

if(date1.getTime() > date2.getTime()) {
  //...
}
问题回答

这里的问题关键在于时间戳被转换为日期对象时。使用了日期对象的“after”方法,而不是时间戳中的“after”方法。若不将其转换为日期对象,则时间戳的“after”方法可以正常运作。

我猜我现在需要一个关于协变方法参数的课程,以便理解为什么以下代码中TimeStamp的后续方法没有被调用。

    java.sql.Timestamp one = new java.sql.Timestamp(1266873627200L);
    java.sql.Timestamp two = new java.sql.Timestamp(1266873627000L);

    java.util.Date oneDate = (java.util.Date) one;
    java.util.Date twoDate = (java.util.Date) two;


    System.out.println("one: " + oneDate.getTime());
    System.out.println("two: " + twoDate.getTime());

    if (oneDate.after(twoDate)) {
        System.out.println(oneDate.getTime() + " after " + twoDate.getTime());
    } else {
        System.out.println(twoDate.getTime() + " after " + oneDate.getTime());
    }

结果

one: 1266873627200
two: 1266873627000
1266873627000 after 1266873627200

看起来你遇到的问题是firstDate和secondDate都设置为java.util.Date对象,但是java.sql.Timestamp的JavaDoc提到它是由java.util.Date和一个单独的纳秒值组成的复合值。

这就是为什么在尝试比较这两个对象时会返回false。如果您将firstDate和secondDate更改为实际的Timestamp对象,则它们应该可以工作。

没有实际代码的情况下,这只是一种猜测,但我认为你的一个对象是java.util.Date,另一个对象是java.sql.Timestamp,你无法真正将它们进行比较,因为在Timestamp中的millis字段会截取到秒,并将其余部分存储在nanos字段中。非常不幸的是,TimestampDate的子类。这绝对会引起你正在经历的问题。

<>strong>EDIT.

另一种可能性是,贵行的司机将自己的下级改为timestamp。 由于这一类别并非最终确定,因此其实施很可能重新篡改了“时间”编码/code>,而不是特别难以做到。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签