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

Comparing only dates and not timestamp

 
Ranch Hand
Posts: 111
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79648
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bill and Joanne for your help and also for pointing out my silly mistake.
 
It is an experimental device that will make my mind that most powerful force on earth! More powerful than this tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic