PDA

View Full Version : Days in between two dates - general question


Partial
2006-10-18, 22:56
I am working on a program for my object-oriented programming class. We're writing our code in Java.

I'm working on a program that is involving a list of dates. The class we're working in is called TDate, since Java has a built-in date class. I have two dates of type TDate d1 and d2. I need to be able to figure out how many integer days are in between the two dates.

I have greaterThan and lessThan functions within the class which let me know which date is bigger and which is smaller. The problem is I am not sure how I would go about keeping an active count on the dates because the months really throw things off.

For example,
11/25/2002 is considered "less than" 12/15/2003, yet if I were to loop through it the months would clearly not reflect this.

What I do have is a function that keeps track of the last day of the month for me. I am assuming I will need this to validate leap years and such.

public int lastDayOfMonth()
{
switch ( month )
{
case APR : case JUN : case SEP : case NOV :
return 30;

case FEB :
if( year % 400 == 0 || year % 4 == 0 && year % 100 != 0 )
return 29;
return 28;

default:
return 31;
}
}

So, any thoughts? I am rather confused and the program is due at 8:00am Central time Friday morning, so i've got a little time but would rather just get this done.

chucker
2006-10-18, 23:56
I'm not entirely sure what you're asking for. Your lastDayOfMonth() function looks right to me. Now all you need is each one method to compare days, months (which would call lastDayOfMonth()), and years. Each of these methods would increase the overall count appropriately.

I don't write Java, so at best, I could give you pseudo-code, but from how you've done the above, it seems like you could master the problem rather easily?

AsLan^
2006-10-19, 00:49
Just calculate the number of days each date object has from an arbitrary date (such as Jan 1st, 1970) then compare the total days against each other.

EDIT: If Jan 1st, 1970 is too restrictive, go with something like Feb 24th, 1582, or Jan 1st, 1752.

Partial
2006-10-19, 10:42
Thanks for both of your help. AsLan, that seems like a really, really good idea!

Brad
2006-10-19, 10:48
Thanks for both of your help. AsLan, that seems like a really, really good idea!
AsLan's idea is actually how some languages process dates natively.

(although, in many cases, it's the number of seconds since epoch, the beginning baseline date)

chucker
2006-10-19, 10:56
AsLan's idea is actually how some languages process dates natively.

(although, in many cases, it's the number of seconds since epoch, the beginning baseline date)

Just to pick nits: January 1st 1970 is merely the Unix (and thus Mac OS X) epoch beginning. For Mac OS Classic, it's 1904; for Windows, it's 1900. IIRC.

Not that it matters or anything.

Aside from an absurd Excel compatibility problem, that is, whereby all dates end up four years off.

Partial
2006-10-19, 12:43
Aslan, so what you're suggesting is I declare a new instance of TDate and initalize it to something (say august 3rd, 2001), and figure out the relative distances of the two dates being compared by the function from the new instance of TDate, correct?

AsLan^
2006-10-19, 13:09
Umm.... yes?

Actually, I don't know the constraints being placed upon your assignment but when I instantiate a Date object I would also calculate an "absolute" number of days from an arbitrary point and store that figure in a data member and include a getDifferenceFrom(Date d) method.

Of course, if you can't modify the Date class you've been given to work with then you will have to figure out the absolute number of days at run time with some kind of static function (method).

Remember to to include some kind of check to ensure that a lower number is being subtracted from a higher number.

To calculate the number of days for a date object just follow your rules for determining whether any given year is a leap year, and whether a month is 31, 30, 29, or 28 days long.

int i = 1970;
days = 0;

while (i < year)
{

if (i == a leap year)
{
days = days + 366;
}
else
{
days = days + 365;
}

i++;
}

Then repeat for months and days. This will give you an absolute days which can then be used to subtract (or be subtracted from) from another Date object to give the difference between Dates.