halukakin79
asked on
Finding day difference between two dates
Hello,
I've been trying to figure how to correctly find the day difference between two days.
Below you will see a code I'm using to compare days. But I believe it's not functioning correctly. I'm testing it on these two dates:
2005-08-10 and 2005-02-10
The code below says the difference is 184 days.
However when I use excel's day difference function (namely days360) excel says the difference is 180 days.
I would like to learn how to find the difference correctly.
Thanks!
xxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx xx
public static int getDaysBetween (java.util.Calendar d1, java.util.Calendar d2) {
if (d1.after(d2)) { // swap dates so that d1 is start and d2 is end
java.util.Calendar swap = d1;
d1 = d2;
d2 = swap;
}
int days = d2.get(java.util.Calendar. DAY_OF_YEA R) -
d1.get(java.util.Calendar. DAY_OF_YEA R);
int y2 = d2.get(java.util.Calendar. YEAR);
if (d1.get(java.util.Calendar .YEAR) != y2) {
d1 = (java.util.Calendar) d1.clone();
do {
days += d1.getActualMaximum(java.u til.Calend ar.DAY_OF_ YEAR);
d1.add(java.util.Calendar. YEAR, 1);
} while (d1.get(java.util.Calendar .YEAR) != y2);
}
return days;
} // getDaysBetween()
I've been trying to figure how to correctly find the day difference between two days.
Below you will see a code I'm using to compare days. But I believe it's not functioning correctly. I'm testing it on these two dates:
2005-08-10 and 2005-02-10
The code below says the difference is 184 days.
However when I use excel's day difference function (namely days360) excel says the difference is 180 days.
I would like to learn how to find the difference correctly.
Thanks!
xxxxxxxxxxxxxxxxxxxxxxxxxx
public static int getDaysBetween (java.util.Calendar d1, java.util.Calendar d2) {
if (d1.after(d2)) { // swap dates so that d1 is start and d2 is end
java.util.Calendar swap = d1;
d1 = d2;
d2 = swap;
}
int days = d2.get(java.util.Calendar.
d1.get(java.util.Calendar.
int y2 = d2.get(java.util.Calendar.
if (d1.get(java.util.Calendar
d1 = (java.util.Calendar) d1.clone();
do {
days += d1.getActualMaximum(java.u
d1.add(java.util.Calendar.
} while (d1.get(java.util.Calendar
}
return days;
} // getDaysBetween()
http://forum.java.sun.com/thread.jspa?threadID=488676&messageID=2292039
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Try that,
I have not tested it :)
I have not tested it :)
ASKER
No that doesn't do it.
I'm testing it between 2005-02-10 and 2005-03-10.
Your code says there are 30 days in between.
However when I count the days from the calendar there are 28 days in between.
I'm testing it between 2005-02-10 and 2005-03-10.
Your code says there are 30 days in between.
However when I count the days from the calendar there are 28 days in between.
can you post how do you construct your calendar?
ASKER
Sure thing. Here it is:
Calendar c1 = Calendar.getInstance();
c1.set(2005, 3 , 10);
Calendar c2 = Calendar.getInstance();
c2.set(2005, 2 , 10);
getDaysBetween(c1,c2);
Calendar c1 = Calendar.getInstance();
c1.set(2005, 3 , 10);
Calendar c2 = Calendar.getInstance();
c2.set(2005, 2 , 10);
getDaysBetween(c1,c2);
Give me a sec
Do you know that the month is actually starting from zero?
So FEBRUARY is 1 instead of 2
Calendar c1 = Calendar.getInstance();
c1.set(2005, Calendar.MARCH , 10);
Calendar c2 = Calendar.getInstance();
c2.set(2005, Calendar.FEBRUARY , 10);
is equal to
Calendar c1 = Calendar.getInstance();
c1.set(2005, 2 , 10);
Calendar c2 = Calendar.getInstance();
c2.set(2005, 1 , 10);
getDaysBetween(c1,c2);
So FEBRUARY is 1 instead of 2
Calendar c1 = Calendar.getInstance();
c1.set(2005, Calendar.MARCH , 10);
Calendar c2 = Calendar.getInstance();
c2.set(2005, Calendar.FEBRUARY , 10);
is equal to
Calendar c1 = Calendar.getInstance();
c1.set(2005, 2 , 10);
Calendar c2 = Calendar.getInstance();
c2.set(2005, 1 , 10);
getDaysBetween(c1,c2);
Use
System.out.println(c1.getT ime().toSt ring());
System.out.println(c2.getT ime().toSt ring());
to have a look what I mean
David
System.out.println(c1.getT
System.out.println(c2.getT
to have a look what I mean
David
ASKER
Thanks a lot! :)
You are welcome :)
Glad I could help
David
Glad I could help
David
long start = cal1.getTime().getTime();
long end - cal2.getTime().getTime();
long diff = (end - start)/86400;
This should work irrespective of the timezones.
Kumar
www.fwanalyzer.com
long end - cal2.getTime().getTime();
long diff = (end - start)/86400;
This should work irrespective of the timezones.
Kumar
www.fwanalyzer.com