File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Comparing only dates and not timestamp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Comparing only dates and not timestamp" Watch "Comparing only dates and not timestamp" New topic
Author

Comparing only dates and not timestamp

Abdul Mohsin
Ranch Hand

Joined: Apr 26, 2007
Posts: 111

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.


Regards, Abdul Mohsin
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
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

Joined: Nov 08, 2007
Posts: 457


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 )


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Abdul Mohsin
Ranch Hand

Joined: Apr 26, 2007
Posts: 111

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

Joined: Aug 05, 2005
Posts: 3169
    
  10
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.


Joanne
Abdul Mohsin
Ranch Hand

Joined: Apr 26, 2007
Posts: 111

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

Joined: Aug 05, 2005
Posts: 3169
    
  10
Try
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
Abdul,
Your order of operations got you in trouble.

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

you wanted

a / (b * c)

Abdul Mohsin
Ranch Hand

Joined: Apr 26, 2007
Posts: 111

Thanks Bill and Joanne for your help and also for pointing out my silly mistake.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Comparing only dates and not timestamp
 
Similar Threads
Set holding two dates
timespan false?
what's wrong with this,.. Date, Calendar Stuff.. ????
dates between two dates
compare date in jsp