• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Date Calculations - Difference between 2 dates is off

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
MySQL Database Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

according to the method names
Jamie
[ October 15, 2002: Message edited by: Jamie Robertson ]
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for date difference, how about:

Jamie
[ October 15, 2002: Message edited by: Jamie Robertson ]
 
Robert Popular
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Are we home yet? Wait, did we forget the tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic