java. sql. Timestamp comparison error?
See English answer > java sql. Timestamp created from java. util. Date,why always before() it? two
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)));
The output is as follows:
Date1: 2013-03-26 11:04:01.093 Date2: 2013-03-26 11:04:01.103 Comparing times d1.t < d2.t: true Comparing dates d1.before(d2): false
This Java sql. What happened to the timestamp class?
Yes, I see this:
But its date is < - > time
In my example, I only have time stamps, and the behavior is unexpected
Update: answer
The reason for this is that the before () method is overloaded and will not be used in Java sql. Overridden in timestamp I look forward to "transcendence" behavior The correct way to compare timestamps is to use the timestamp variable instead of the date
This is still a bad design decision in the Java core, because inheritance should mean that timestamp is a date without penalties and exceptions
Solution
Try using timestamp instead of date, it will work
Timestamp d1 = new java.sql.Timestamp(new Date().getTime());
Timestamps and dates have their own methods of execution Verify