• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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

 
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because the time within the dates are likely different.
 
Marshal
Posts: 8960
646
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79956
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79956
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79956
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Eat that pie! EAT IT! Now read this tiny ad. READ IT!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic