Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Two java.sql.Date Not Equal By compareTo() Function  RSS feed

 
sudipto shekhar
Ranch Hand
Posts: 826
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have the following code:



The output of Line 1 and Line 2 is :

2016-07-07
2016-07-07

The expected output of Line 3 is '0' but the output of the code is '-1'. I am not able to figure out the reason.

Please help!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66208
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the time within the dates are likely different.
 
Liutauras Vilda
Marshal
Posts: 4670
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.

Returns the current time in milliseconds. That time is captured when line1 gets executed.
Returns the current time in milliseconds. That time is captured when line6 gets executed.

When you trying to print sql.Date object it invokes toString() method overridden by that class which says "Formats a date in the date escape format yyyy-mm-dd."
So, since two different code lines manage to run on the same day and only milliseconds differ, you see same date, while behind the hood it isn't.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Useful way to time a print call. The time it takes to contact the command line/terminal via the Operating System is obviously long enough to make a difference.
Avoid classes like Date and Calendar as much as possible. Use the new Java8 date/time classes whenever you can. You probably only need java.sql.Date when you are dealing directly with databases.
 
sudipto shekhar
Ranch Hand
Posts: 826
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I figured this out eventually.

What I am trying to achieve here is compare a java.sql.Date to the current system date; if the sql date is before the current system date the check shall return TRUE. But this check becomes TRUE even if the date is equal to current system date (this check should return false).

Any effective way to achieve this?

Kindly suggest.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using < 0 or <= 0 in your comparisons? You should show us your code, so we know what is going on.
Let's try getting rid of the legacy code.An Instant object encapsulates the time elapsed since 1st January 1970, just as a Date object does, but it multiplies the milliseconds by 1000000 to make nanoseconds. Now let's see if we can't get a current time Instant object. Easy: use its now() method and Bob's your uncle:-
 
sudipto shekhar
Ranch Hand
Posts: 826
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apologies as I forgot to mention that I am on Java6.

I am using date.before(anotherDate) method to compare two dates.

Moreover, Instant is a class that I could not find. Please let me know what am I missing.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you still on Java6? That has been obsolete for a couple of years. I said in a link yesterday,
the new Java8 date/time classes
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!