• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ketan Mohite
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the responses guys. I understand the cause of the issue that I was facing, and commented the same in my second post.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic