• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Comparing only dates and not timestamp

 
Abdul Mohsin
Ranch Hand
Posts: 111
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to compare two dates neglecting there timestamp, means if two dates point to same 11th Feb then they should be equals (date1.after(date2) and date1.before(date2) both should be false).


I want the better way of doing it , currently I am doing it as setting hour,minute,second,millisecond as 0 and AM to both of the times and then comparing it.

Please suggest if there is a better way of doing it.
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried a Comparator? You can pass the day month year part and compare them alone.
It is quite permissible for a Comparator to return 0 when the equals() method returns false.
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


That should work.
Put it in its own method if you want it to be cleaner.

Those methods are marked as deprecated in Java 5.0 - not sure what th suggested new way should be. I think it involves Calendar.


(aside: English note - "I want to compare two dates neglecting their timestamps", I'm not trying to be rude, there's a lot of English as a second language on The Ranch, and a lot of "what should I learn for a job interview", and better written and oral communications are at least as high as programming skills!, and their/there/they're is a common English problem, and a nitpick of many people (including me )
 
Abdul Mohsin
Ranch Hand
Posts: 111
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bill,

Sorry for grammatical mistakes, your code is only checking for the equivalence of two dates and not doing any comparison, I need to compare and then perform some operation.

Currently I am implementing the logic as:


but I am not happy with this current implementation and want to improve it further.

Please suggest.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried dividing the value returned by getTimeInMillis by the number of milliseconds in a day ? Try it for various Calendar objects that represent different times on the same day. Compare the results and see if you can see a way to make use of them.
 
Abdul Mohsin
Ranch Hand
Posts: 111
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joanne,

Can you please explain your approach further with code,
I tried your approach

// output 12960000000000 , refer the code posted earlier
but it didn't worked.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abdul,
Your order of operations got you in trouble.

a / b * c == (a / b) * c

you wanted

a / (b * c)

 
Abdul Mohsin
Ranch Hand
Posts: 111
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bill and Joanne for your help and also for pointing out my silly mistake.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic