java.sql.Timestamp comparison bug?

richw

New Member
Hello I have a code snippet like this:\[code\]Date d1 = new java.sql.Timestamp(new Date().getTime());Thread.sleep(10);Date d2 = new java.sql.Timestamp(new Date().getTime());System.out.println("Date1: " + d1);System.out.println("Date2: " + d2);System.out.println("Comparing times d1.t < d2.t: " + (d1.getTime() < d2.getTime()));System.out.println("Comparing dates d1.before(d2): " + (d1.before(d2)));\[/code\]The output looks like this:\[code\]Date1: 2013-03-26 11:04:01.093Date2: 2013-03-26 11:04:01.103Comparing times d1.t < d2.t: trueComparing dates d1.before(d2): false\[/code\]What's wrong with this java.sql.Timestamp class?Yes, I have seen this:\[quote\] Note: This type is a composite of a java.util.Date and a separate nanoseconds value. Only integral seconds are stored in the java.util.Date component. The fractional seconds - the nanos - are separate. The Timestamp.equals(Object) method never returns true when passed a value of type java.util.Date because the nanos component of a date is unknown. As a result, the Timestamp.equals(Object) method is not symmetric with respect to the java.util.Date.equals(Object) method. Also, the hashcode method uses the underlying java.util.Date implementation and therefore does not include nanos in its computation. Due to the differences between the Timestamp class and the java.util.Date class mentioned above, it is recommended that code not view Timestamp values generically as an instance of java.util.Date. The inheritance relationship between Timestamp and java.util.Date really denotes implementation inheritance, and not type inheritance.\[/quote\]But it's for Date <-> Timestamp relation.In my example I have Timestamps only, and still the behavior is unexpected..
 
Back
Top