code to get number of days given a start and an end date

I have written down a code for getting number of days...

I am not getting any errors but i am not getting any answers as well...

import java.util.*;

import java.text.*;

public class Calculate {

public static void main(String args[]) {

try {

double result=0;

String d1="07/11/2007";

String d2="07/14/2007";

Date date1,date2;

SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy");

date1=formatter.parse(d1);

date2=formatter.parse(d2);

Calendar leaveStartDate = Calendar.getInstance();

Calendar leaveEndDate = Calendar.getInstance();

leaveStartDate.setTime(date1);

leaveEndDate.setTime(date2);

leaveEndDate.add(Calendar.DATE,1);

while(leaveStartDate.before(leaveEndDate)) {

if(leaveStartDate.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY &&

leaveStartDate.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY)

leaveStartDate.add(Calendar.DATE,1);

result=result+1;

}

System.out.println("No of days "+result);

}

catch(ParseException e) {

System.err.println(e);

}

}

Thanks in advance.

Puneet.

[1220 byte] By [Puneet_IDSa] at [2007-11-27 10:31:03]
# 1

Should notif(leaveStartDate.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY &&

leaveStartDate.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY)

leaveStartDate.add(Calendar.DATE,1);

result=result+1;

beif(leaveStartDate.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY &&

leaveStartDate.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY)

result=result+1;

leaveStartDate.add(Calendar.DATE,1);

sabre150a at 2007-7-28 18:06:36 > top of Java-index,Java Essentials,Java Programming...
# 2

take the two dates in milliseconds (using getTime()), subtract and divide by the number of milliseconds in a day which is 24 * 3600000. Round the result (otherwise you can get problems due to change to and from DST.)

malcolmmca at 2007-7-28 18:06:36 > top of Java-index,Java Essentials,Java Programming...
# 3

Thanks for that sabre...

It is giving me the answer but i think it is coming 1 day short of what i should actually get...

And plus i if the month changes for ex. start date is 27th July and end date is 4th Aug then i am getting the result as 1.0

Can you pls check?

Thx again.

Message was edited by:

Puneet_IDS

Puneet_IDSa at 2007-7-28 18:06:36 > top of Java-index,Java Essentials,Java Programming...
# 4

Calendar really isn't designed to work that way arround. Infinitely simpler and more reliable simply to subtract the epoch times.

malcolmmca at 2007-7-28 18:06:36 > top of Java-index,Java Essentials,Java Programming...
# 5

> Thanks for that sabre...

>

> It is giving me the answer but i think it is coming 1

> day short of what i should actually get...

>

> And plus i if the month changes for ex. start date is

> 27th July and end date is 4th Aug then i am getting

> the result as 1.0

> Can you pls check?

>

> Thx again.

>

> Message was edited by:

> Puneet_IDS

:-) You need to look at your SimpleDateFormat parser string!

sabre150a at 2007-7-28 18:06:36 > top of Java-index,Java Essentials,Java Programming...
# 6

> Calendar really isn't designed to work that way

> arround. Infinitely simpler and more reliable simply

> to subtract the epoch times.

I have to disagree with this. The number of days between two Dates is not just the number_of_milliseconds / number_of_milliseconds_per_day since this does not take into account daylight saving time and leap seconds.

It also does not take into account Saturdays and Sundays as the OP requires.

Message was edited by:

sabre150

sabre150a at 2007-7-28 18:06:36 > top of Java-index,Java Essentials,Java Programming...
# 7

> > Calendar really isn't designed to work that way

> > arround. Infinitely simpler and more reliable

> simply

> > to subtract the epoch times.

>

> I have to disagree with this. The number of days

> between two Dates is not just the

> number_of_milliseconds /

> number_of_milliseconds_per_day since this does not

> take into account daylight saving time and leap

> seconds.

If the dates are supplied with zero in the time fields then rounding the result of the division takes care of such things nicely.

If they have actual times attached then you have to define exactly what the difference in days between two timestamps actually means, where DST is involved it can get ambiguous. Genarally, though, the zero time version is what people intuitively mean.

malcolmmca at 2007-7-28 18:06:36 > top of Java-index,Java Essentials,Java Programming...