Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# Date Calculations - Difference between 2 dates is off

Robert Popular
Greenhorn
Posts: 6
Hi,
I have been looking for code examples of how to take 2 dates, compare them to see if the difference is > or < than a desired value, then perform an action.
Here is the code I am using so far:

In my main app, I am executing the following:

The problem is, for comparing the time between 01.01.2002 and 06.30.2002, the result is 179 instead of 181. For 07.01.2002 to 12.31.2002, the result is 183 instead of 184. Where am I losing the accuracy here? I tried converting them to floats in order to make sure that rounding wasn't affecting this, but no luck.
Can someone tell me the best practice for doing this? I would think this is a fairly common action that must be performed in programs.
bp

Jamie Robertson
Ranch Hand
Posts: 1879
getMilliDelta(Date d1, Date d2) can be shortened to

I think I have a date diff function laying around here somewhere, I'll try to dig it up..
Jamie
[ October 15, 2002: Message edited by: Jamie Robertson ]

Cindy Glass
"The Hood"
Sheriff
Posts: 8521
You aren't using the getMilliDelta() method that you showed us. Show us the getSecondDelta() method that you ARE using.

Jamie Robertson
Ranch Hand
Posts: 1879

according to the method names
Jamie
[ October 15, 2002: Message edited by: Jamie Robertson ]

Jamie Robertson
Ranch Hand
Posts: 1879
for date difference, how about:

Jamie
[ October 15, 2002: Message edited by: Jamie Robertson ]

Robert Popular
Greenhorn
Posts: 6
Thanks for the responses. Just replace the method name with the one I originally posted about - getMilliDate()
Also, I tried the diff method and it is still off by a day on the July-Dec 180 day measurement. Should return 184 and returns 183.
Not sure what else to try.
bp

Tom Hughes
Ranch Hand
Posts: 86

public int datediff( Date d1, Date d2 ){ return Math.abs( (int)( ( d1.getTime() - d2.getTime() )/(1000*60*60*24) ) );}

This doesn't take account of leap years.

Robert Popular
Greenhorn
Posts: 6
[CODE]
SimpleDateFormat sdf = new SimpleDateFormat("yyyymmdd");
java.util.Date date = null;
try
{
date = sdf.parse("20021010");
System.out.println("sdf=" +date.toString());
}
catch(ParseException pe)
{
pe.printStackTrace();
}
Then I can try something like...

if(date.compareTo(currentDate) = true)
{
}
...
[\CODE]
The problem is that the date format that my query is returning is based on Novel eDir8.6.2/NDS.
For example, 20021010152748Z.
I would love to create a SimpleDateFormat for this and then just blow right on through. Is this
a date format that many are familiar with?
Thanks again!
bp

Jamie Robertson
Ranch Hand
Posts: 1879
Originally posted by Tom Hughes:

This doesn't take account of leap years.

I think that it doesn't need to take into account leap years, since it is only measuring units of time in days ( which day isn't important ). The number represents a measurement of one day. You might be seeing some discrepancies due to the precision of hours, minutes, seconds and so forth being used in the calculation, when you only want the precision to be days. If you make the dates using Calendars and set the hours, minutes, seconds and milliseconds to 0, you might solve the problem. If you want us to look at the details, post the code that is producing the faulty results ( especially the date creation part ).
Jamie