Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2005

Tip: Looking for answers? Try searching our database.

Date difference

Thread view: 
mscout1@techie.com - 20 Feb 2005 00:39 GMT
I need to calculate the difference between two dates stored in Calender
objects. The difference should be represented as a number of days and
rounded down to a whole number of days.

Is there a smarter way of doing this than using a while loop to
incriment the earler date by one day at a time until thay match?

Sorry if this is a trivial question, I am still learning Java.
Thanks in advance.
Bjorn Abelli - 20 Feb 2005 01:08 GMT
<mscout1@techie.com> wrote...

>I need to calculate the difference between two dates stored in Calender
> objects. The difference should be represented as a number of days and
[quoted text clipped - 5 lines]
> Sorry if this is a trivial question, I am still learning Java.
> Thanks in advance.

This is cut out of something I wrote a couple of years ago, slightly
modified to be more "illustrative"...

...

private static final long ONE_DAY = 24 * 60 * 60 * 1000;

...

public long difference(Date d1, Date d2)
{
  long thisDate  = d1.getTime();
  long otherDate = d2.getTime();

  long diff = thisDate - otherDate;
  long days = diff / ONE_DAY;

  return days;
}

// Bjorn A
mscout1@techie.com - 20 Feb 2005 02:06 GMT
So fir Calenders in would be :

private static final long ONE_DAY = 24 * 60 * 60 * 1000;
private static int deltaDate(Calendar start, Calendar finish)
{

    long thisDate  = finish.getTime().getTime();
    long otherDate = start.getTime().getTime();

    long diff = thisDate - otherDate;
    long days = diff / ONE_DAY;

    return days;
}

Hmm..  It's still ugly but at least it gets the job done w/o taking
O(n) time.
I was hoping for a hidden library function, but This works too.

Thanks.
Bjorn Abelli - 20 Feb 2005 12:43 GMT
<mscout1@techie.com> wrote...
> So fir Calenders in would be :
>
[quoted text clipped - 4 lines]
> long thisDate  = finish.getTime().getTime();
> long otherDate = start.getTime().getTime();

You can eliminate the redundant call for Dates by going for the milliseconds
directly.

 long thisDate  = finish.getTimeInMillis();
 long otherDate = start.getTimeInMillis();

> long diff = thisDate - otherDate;
> long days = diff / ONE_DAY;
>
> return days;
> }

// Bjorn A
mscout1@techie.com - 20 Feb 2005 22:50 GMT
Ah, Much better! I missed that method the in the API or I would have
done it that way in the first place.
Tony Dahlman - 21 Feb 2005 04:04 GMT
> I need to calculate the difference between two dates stored in Calender
> objects. The difference should be represented as a number of days and
[quoted text clipped - 5 lines]
> Sorry if this is a trivial question, I am still learning Java.
> Thanks in advance.

Not trivial at all.  Dates are as tricky as they are tough to work with,
no matter what the programming language.

If what you have so far from previous posts doesn't help, you might want
to try "reverting" to the Date objects and subtract them using:

    http://pws.prserv.net/ad/programs/Programs.html#DateDiff

This code acknowledges that a year, and therefore a day, is actually
longer than 365.0 days, etc.

I'm confident that you will not be able to find a precise and exact
answer to your question, but that is the nature of dates and calendars.
OTOH you may find that this solution meets your requirements.

Regards --Tony Dahlman

Signature

---------------------------------------
nospam, I've had it, thanks.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.