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 Date Calculations - Difference between 2 dates is off Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Date Calculations - Difference between 2 dates is off" Watch "Date Calculations - Difference between 2 dates is off" New topic
Author

Date Calculations - Difference between 2 dates is off

Robert Popular
Greenhorn

Joined: Oct 14, 2002
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.
Thanks in advance!
bp
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
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

Joined: Sep 29, 2000
Posts: 8521
You aren't using the getMilliDelta() method that you showed us. Show us the getSecondDelta() method that you ARE using.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879


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

Joined: Jul 09, 2001
Posts: 1879

for date difference, how about:

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

Joined: Oct 14, 2002
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

Joined: Feb 09, 2002
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

Joined: Oct 14, 2002
Posts: 6
Ok, how about this...
[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...
date.add(Calendar.ADD, 180);

if(date.compareTo(currentDate) = true)
{
send notification
}
...
[\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

Joined: Jul 09, 2001
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Date Calculations - Difference between 2 dates is off
 
Similar Threads
Number of weeks between two dates
how to find how many sundays comes in 2 Dates
Number of days between dates
regarding iteration
Difference between 2 date in day