This discussion is archived
11 Replies Latest reply: Mar 18, 2008 12:01 PM by 807601

No. of working days between given two dates

Currently Being Moderated
Hi all,
I want to count the No. of working days between two dates.
I want to calculate monday to friday (working days).
I have two dates i.e fromDate & toDate. I have to calculate the working days.

can anybody ,help me.

• 1. Re: No. of working days between given two dates
Currently Being Moderated
this is weekdays, not working days

create 2 calendar objects
then
while(fromDate.before(toDate)) ***see below
fromDate get the DAY_OF_WEEK % 7
if > 1weekdays++

*** while 'condition' depends on whether days are inclusive or exclusive
e.g could be !fromDate.after(toDate)
• 2. Re: No. of working days between given two dates
Currently Being Moderated
create 2 calendar objects
then
while(fromDate.before(toDate)) ***see below
fromDate get the DAY_OF_WEEK % 7
if > 1weekdays++

*** while 'condition' depends on whether days are
inclusive or exclusive
e.g could be !fromDate.after(toDate)
Thanks

please, can you send the full code.
• 3. Re: No. of working days between given two dates
Currently Being Moderated
please, can you send the full code.
• 4. Re: No. of working days between given two dates
Currently Being Moderated
please, can you send the full code.
Thanks,

i'll try.

(Before u say i cant, say i will try --vivekananda)
• 5. Re: No. of working days between given two dates
Currently Being Moderated
Here is the complete code to calculate the working days and total no. of days...............
``````/*
* Created on Jun 28, 2006
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package org.worldbank.rpms.helper;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
* @author WB308415 srireddy
*
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class DateDifference {
public static void main(String[] args) {
Date fromDate = new Date(System.currentTimeMillis());
// fromDate is "System Current Date"
Calendar calendar = Calendar.getInstance();
calendar.set(2006, 10, 06);
/*
* 2006:-> Year
* 10  :-> November
* 06  :-> Date
*/
Date toDate = calendar.getTime();
System.out.println("Date Difference");
System.out.println("from " + fromDate);
System.out.println(" to  " + toDate);
System.out.println(" Total days are: "
+ getDateDifferenceInDays(fromDate, toDate) + " Days.");
System.out.println(" Total No. of working days are : "
+ getDateDifferenceInDaysExcludingSatSun(fromDate, toDate) + " Days.");
}

private static int getDateDifferenceInDays(Date fromDate, Date toDate) {
if (fromDate == null || toDate == null) {
return 0;
}
boolean neglect = false;
int calUnit = Calendar.DATE;
if (fromDate.after(toDate)) {
// swaps the date if fromDate is after toDate
Date temp = fromDate;
fromDate = toDate;
toDate = temp;
neglect = true;
}
// estimate the diff. d1 is now guaranteed <= d2
int estimate = getEstDiff(calUnit, fromDate, toDate);
// convert the Dates to GregorianCalendars
GregorianCalendar calendar1 = new GregorianCalendar();
calendar1.setTime(fromDate);
GregorianCalendar calendar2 = new GregorianCalendar();
calendar2.setTime(toDate);
for (int iterate = estimate - 1;; iterate++) {
if (calendar1.after(calendar2)) {
if (neglect) {
return 1 - iterate;
} else {
return iterate ;
}
}
}
}

private static int getEstDiff(int calUnit, Date d1, Date d2) {
double aDAY_MILLIS = 1000 * 60 * 60 * 24.0015;
double aWEEK_MILLIS = aDAY_MILLIS * 7;
double aMONTH_MILLIS = aDAY_MILLIS * 30.43675;
double aYEAR_MILLIS = aWEEK_MILLIS * 52.2;
long diff = d2.getTime() - d1.getTime();
switch (calUnit) {
case Calendar.DAY_OF_WEEK_IN_MONTH:
case Calendar.DAY_OF_MONTH:
return (int) (diff / aDAY_MILLIS + .5);
case Calendar.WEEK_OF_YEAR:
return (int) (diff / aWEEK_MILLIS + .5);
case Calendar.MONTH:
return (int) (diff / aMONTH_MILLIS + .5);
case Calendar.YEAR:
return (int) (diff / aYEAR_MILLIS + .5);
default:
return 0;
}
}

public static int getDateDifferenceInDaysExcludingSatSun(Date fromDate,Date toDate)
{
if (fromDate == null || toDate == null) {
return 0;
}
if (fromDate.after(toDate)) {
Date temp = fromDate;
fromDate = toDate;
toDate = temp;
}
Calendar calFrom = Calendar.getInstance();
calFrom.setTime(fromDate);
Calendar calTo = Calendar.getInstance();
calTo.setTime(toDate);
int iNoOfWorkingDays = 0;
do {
if (calFrom.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY
&& calFrom.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
iNoOfWorkingDays += 1;
}
} while (calFrom.getTimeInMillis() < calTo.getTimeInMillis());
return iNoOfWorkingDays;
}

}``````
Thanks........................To all
• 6. Re: No. of working days between given two dates
Currently Being Moderated
Gr8 job boss...this solved our problem also.

Rgds,
Sha
• 7. Re: No. of working days between given two dates
Currently Being Moderated
very great thank u very much.......
• 8. Re: No. of working days between given two dates
Currently Being Moderated
Another approach:
``````long millisecsInDay = ( 24 * 60 * 60 * 1000);
Date startDate;
Date endDate;
long startDateMillis = startDate.getTime();
long endDateMillis = endDate.getTime();
long diff = endDateMillis - startDateMillis;
int daysBetween = diff / millisecsInDay;``````
• 9. Re: No. of working days between given two dates
Currently Being Moderated
Another approach:
Not a good one; it's buggy. It's better to stick to the Calendar API.

~
• 10. Re: No. of working days between given two dates
Currently Being Moderated
vinodreddy wrote:
very great thank u very much.......
wtf, you thanked someone for a 2 year old thread?
• 11. Re: No. of working days between given two dates
Currently Being Moderated
i have a simple and great idea
make so that if a thread is old it warns you before posting a reply
(yes i realize this will probably never happen)