aspose file tools*
The moose likes Java in General and the fly likes java.util.Date - after function not considering time? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "java.util.Date - after function not considering time?" Watch "java.util.Date - after function not considering time?" New topic
Author

java.util.Date - after function not considering time?

Ketan Mohite
Ranch Hand

Joined: Feb 14, 2006
Posts: 45
Hi All,

I am working on a Java application which retrieves two dates (of type datetime) from database table. e.g. 2009-05-13 11:11:03.113 and 2009-05-13 11:11:03.257

I am saving these into two variables date1 and date2 of type java.util.Date.

i.e. date1 --> 2009-05-13 11:11:03.113
date2 --> 2009-05-13 11:11:03.257

As seen these two dates are equal but there is difference in time : 113 v/s 257

I use the following function,
boolean bool1 = date2.after(date1);
boolean bool2 = date2.before(date1);

bool1 and bool2 both contain false values (ideally, bool2- false and bool1 - true)

This was with respect to the values obtained from database.

I tried the following code, but created dates in Java code:
[color=blue]
Calendar cal1 = Calendar.getInstance();
cal1.clear();

cal1.set(Calendar.YEAR, 2009);
cal1.set(Calendar.MONTH, 4);
cal1.set(Calendar.DATE, 13);
cal1.set(Calendar.HOUR, 11);
cal1.set(Calendar.MINUTE, 11);
cal1.set(Calendar.SECOND, 03);
cal1.set(Calendar.MILLISECOND, 113);

java.util.Date utilDate1 = cal1.getTime();
// System.out.println(utilDate1);

Calendar cal2 = Calendar.getInstance();
cal2.clear();

cal2.set(Calendar.YEAR, 2009);
cal2.set(Calendar.MONTH, 4);
cal2.set(Calendar.DATE, 13);
cal2.set(Calendar.HOUR, 11);
cal2.set(Calendar.MINUTE, 11);
cal2.set(Calendar.SECOND, 03);
cal2.set(Calendar.MILLISECOND, 257);

java.util.Date utilDate2 = cal2.getTime();
// System.out.println(utilDate2);

if(utilDate1.after(utilDate2))
System.out.println("1 is > than 2");
else
if(utilDate1.before(utilDate2))
System.out.println("1 is < than 2");[/color]

This time the console printed "1 is < than 2", which is correct.

Can someone please suggest what is happening in first case?
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

java.util.Date class checks up to seconds, so your two dates are equal (i.e: they differ from milliseconds). Check java.sql.Date also.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Ketan Mohite
Ranch Hand

Joined: Feb 14, 2006
Posts: 45
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.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

Ketan Mohite wrote:I don't see that java.util.Date class checks up to seconds.

It does.You can try it simply running following code inside a main method,



If you uncomment the commented lines you will get the output true, otherwise false (i.e: second Date object is one second after the first one).
Moojid Hamid
Ranch Hand

Joined: Mar 07, 2009
Posts: 120
Are we really talking about java.util.Date? I am guessing since you are getting it from DB it is a java.sql.Date. The Date class in sql package is a subclass of one in the util package but it zeros out the hours, minutes, seconds and milliseconds. Use java.sql.Timestamp instead to retain the full precision of time.
Moojid Hamid
Ranch Hand

Joined: Mar 07, 2009
Posts: 120
Vijitha Kumara wrote:
Ketan Mohite wrote:I don't see that java.util.Date class checks up to seconds.

Yes, It does.You can try it simply running following code inside a main method,



If you uncomment the commented lines you will get the output true, otherwise false (i.e: second Date object is one second after the first one).


That hardly proves your point. Look at the javadoc for java.util.Date it clearly states that Date has millisecond precision. The reason that you get true in your example is that the your computer is running fast enough two create two Date objects in the same millisecond, since the Date is precise up to millisecond only they are equal. Millisecond may seem very short to you but to your computer it is quite a long time period.
Ketan Mohite
Ranch Hand

Joined: Feb 14, 2006
Posts: 45
I meant to say that java.util.Date class also checks milliseconds, not just upto seconds.

In your code, you can reduce the sleep period and check the same.
Ketan Mohite
Ranch Hand

Joined: Feb 14, 2006
Posts: 45
Thanks for the responses guys. I understand the cause of the issue that I was facing, and commented the same in my second post.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: java.util.Date - after function not considering time?