This discussion is archived
13 Replies Latest reply: May 26, 2005 8:04 AM by JosAH

# Problem with calculate difference between two dates!!!

Currently Being Moderated
Hi
I created a date class with all the functionalities that is getDate, SetDate, if year divisible by 4 and 100 - leap year, if jan, march, may .... 31 days etc...
I have created a date in my main method.

Code:

DateClass today = new DateClass();
today.setDate( 19 , 11 , 1985 );

MY PROBLEM is that I have other dates and I need to call that date (i.e today's date) in my DateClass, in a method. How do I do that?
I have to calculate the difference in days between two dates.
I wanted to get the two dates and see if there is any difference in years, if so, add 365(normal years) or 366(leap years), see if there is any difference in months then calculate the difference in days ......
I someone has a better idea of how to do it you are welcome.
• ###### 1. Re: Problem with calculate difference between two dates!!!
Currently Being Moderated
please search in the fourn. i assure , you will get tons of code . it has been asked many times. basically you need to convert date into milliseconds . ...then subtract two milliseconds and convert the result back into days .
• ###### 2. Re: Problem with calculate difference between two dates!!!
Currently Being Moderated
Eventhough java.util.Date is available why do you want your own Date class?
Why dont you make use of existing utilities?
• ###### 3. Re: Problem with calculate difference between two dates!!!
Currently Being Moderated
This works for me:
``````     /** Returns the actual difference between the two dates in days. If date1 is before date 2,
*  the result will be a positive int. If date1 is after date2, the result will be a negative
*  int.
*
*  @param date1 the first date
*  @param date2 the second date
*     @return the number of days between the two dates.
*/
public static int actualDifference(java.util.Date date1, java.util.Date date2) {
GregorianCalendar gc1 = new GregorianCalendar();
GregorianCalendar gc2 = new GregorianCalendar();
gc1.setTime(date1);
gc2.setTime(date2);

long millies = gc2.getTimeInMillis() - gc1.getTimeInMillis();
return (int) (millies / 1000 / 24 / 60 / 60);
}``````
• ###### 4. Re: Problem with calculate difference between two dates!!!
Currently Being Moderated
I'd use the GregorianCalender class instead, but if you really feel like reinventing
the wheel again, google for 'Zeller congruence'. This little formula calculates
the day of the week, but if you leave out the modulo seven stuff, it calculates
an alternative for the Julian day number. If you calculate the Julian day number
for two dates and subtract the two, you'll get the difference between those two
dates. I'd still go for the GregorianCalender class though ...

kind regards,

Jos
• ###### 5. Re: Problem with calculate difference between two dates!!!
Currently Being Moderated
Hm, this code is quite old and now I'm really wondering why I'm using GregorianCalendar in it...
• ###### 6. Re: Problem with calculate difference between two dates!!!
Currently Being Moderated
Hm, this code is quite old and now I'm really
wondering why I'm using GregorianCalendar in it...
I apologize, I was replying to the OP ... I should've quoted, but when I read the
article, no-one had responded yet. After I pressed the [post] button I saw all
those other replies ... that shows again that I'm a slow old sod ;-)

kind regards,

Jos
• ###### 7. Re: Problem with calculate difference between two dates!!!
Currently Being Moderated
Thanks to all of you!!!
but it's an assignment I have to create a date class.

from
Esroh
• ###### 8. Re: Problem with calculate difference between two dates!!!
Currently Being Moderated
Hi
I created a date class with all the functionalities
that is getDate, SetDate, if year divisible by 4 and
100 - leap year, if jan, march, may .... 31 days
etc...
I have created a date in my main method.
[snip]
MY PROBLEM is that I have other dates and I need to
call that date (i.e today's date) in my DateClass, in
a method. How do I do that?
I don't understand you here. You want to use today's date in your DateClass? Just put the code you have here in the method in your DateClass where you need it.
I have to calculate the difference in days between
two dates.
I wanted to get the two dates and see if there is any
difference in years, if so, add 365(normal years) or
366(leap years), see if there is any difference in
months then calculate the difference in days ......
I someone has a better idea of how to do it you are
welcome.
What are you using to represent a "Date" in your DateClass? Three ints, day in month, month and year? If so then I suppose your plan sounds ok, I'd look into Jos's suggestion though - it sounds interesting.

Good Luck

Lee
• ###### 9. Re: Problem with calculate difference between two dates!!!
Currently Being Moderated
There are many more though if you google a bit.

kind regards,

Jos
• ###### 10. Re: Problem with calculate difference between two dates!!!
Currently Being Moderated
This works for me:
``````/** Returns the actual difference between the two
o dates in days. If date1 is before date 2,
*  the result will be a positive int. If date1 is
is after date2, the result will be a negative
*  int.
*
*  @param date1 the first date
*  @param date2 the second date
*     @return the number of days between the two
wo dates.
*/
public static int actualDifference(java.util.Date
e date1, java.util.Date date2) {
GregorianCalendar gc1 = new GregorianCalendar();
GregorianCalendar gc2 = new GregorianCalendar();
gc1.setTime(date1);
gc2.setTime(date2);

long millies = gc2.getTimeInMillis() -
- gc1.getTimeInMillis();
return (int) (millies / 1000 / 24 / 60 / 60);
}``````
I found a problem:
every month is supposed month with 31 days.

you have an idea how to correct that?

Exemple : the differece ( 2007-02-28 - 2007-03-01 )
is 4 days ....
• ###### 11. Re: Problem with calculate difference between two dates!!!
Currently Being Moderated
> I found a problem:

Two years later (posted May 26, 2005).

~
• ###### 12. Re: Problem with calculate difference between two dates!!!
Currently Being Moderated
to yawmark : in fact , it's only today when i look and find it :)

for the problem, it's just I didn't notice that moths begin with 0 for January and not 1.
• ###### 13. Re: Problem with calculate difference between two dates!!!
Currently Being Moderated
> to yawmark : in fact , it's only today when i look
and find it :)

It always helps to look at the date when you're looking to provide solutions.

> for the problem, it's just I didn't notice that moths
begin with 0 for January and not 1.

For good coding practices, it shouldn't concern you how calendar months are indexed. Just use the Calendar constants; i.e., Calendar.JANUARY for January, etc.

~