Thanks for the reply Vijitha!
I don't see that
java.util.Date class checks up to seconds.
Example posted in the previous post proves it. I am able to compare the dates without issues. The problem occurs when I am retrieving dates from database and the comparing them.
Meanwhile, I have done some interesting analysis on this.
As said, the date values are saved as datetime in database. When obtained them in
java (code given below), we actually get a java.sql.Timestamp. The dates are then casted to java.util.Date as shown.
Timestamp saves milliseconds values in it's "nanos" field, which is not known to java.util.Date class.
When we call 'after' method on these dates, 'after' method of java.util.Date is called, which don't consider 'nanos' and hence giving the results unexpected.
You may refer the following methods
1. in java.util.Date:
2. in java.sql.Timestamp
So, to achieve the required result, we can do either of the following:
1. Type cast the values into Timestamp on retrieval
OR 2. use getTime() method of java.util.Date method for comparision.