Link to home
Start Free TrialLog in
Avatar of vibhav
vibhav

asked on

computing week days between 2 dates

Can i get a code snippet for calculating / computing only week days (excluding saturday and sundays) between two dates (from and to date).

Thanks,
Avatar of StillUnAware
StillUnAware
Flag of Lithuania image

First You should use Calendar instead of Date:

Calendar cal = Calendar.getInstance().setTime(Date);

You can get week number of year:
    cal.get(Calendar.WEEK_OF_YEAR);
day of week:
     cal.get(Calendar.DAY_OF_WEEK);

and so on.

Having this, You can find full weeks passed, and find the days:

(fullweekcount - 1) * 5 + first week day count + last week day count
Avatar of vibhav
vibhav

ASKER

can i get a complete code please...
That will only work on days in the same year...
If, when the end date is on a weekend, you don't want to count a day for it:

    private long getWeekdayDifference(Date startDate, Date endDate) {
        if (startDate.getTime() > endDate.getTime()) return -getWeekdayDifference(endDate, startDate);
       
        Calendar cal = new GregorianCalendar();
        cal.setTime(startDate);
       
        long days = 0;
        while (!cal.getTime().equals(endDate)) {
            int weekDay = cal.get(Calendar.DAY_OF_WEEK);
            cal.add(Calendar.DAY_OF_WEEK,1);
            if (weekDay != Calendar.SATURDAY && weekDay != Calendar.SUNDAY)
                days++;
           
        }
        return days;
    }

If, when the end date is on a weekend, you DO want to count 1 day (as if it were the following monday):

    private long getWeekdayDifference(Date startDate, Date endDate) {
        if (startDate.getTime() > endDate.getTime()) return -getWeekdayDifference(endDate, startDate);
       
        Calendar cal = new GregorianCalendar();
        cal.setTime(startDate);
       
        long days = 0;
        while (!cal.getTime().equals(endDate)) {
            int weekDay = cal.get(Calendar.DAY_OF_WEEK);
            if (weekDay != Calendar.SATURDAY && weekDay != Calendar.SUNDAY)
                days++;
            cal.add(Calendar.DAY_OF_WEEK,1);
        }
        return days;
    }
that is, from a friday to the following saturday, if you want to count that as 1, use the second one, otherwise use the first.
Avatar of vibhav

ASKER

Thanks dberner9.
Your code works fine for me...

I need one more favour.. I need to find end date from given from date which contains 100 working days.

Ex: If start date given is 05/01/2005, end date should be calculated as 09/19/2005.

Can i also get a code snippet for this.
   private long getEndDate(Date startDate, long businessDays) {
        if (startDate.getTime() > endDate.getTime()) return -getWeekdayDifference(endDate, startDate);
       
        Calendar cal = new GregorianCalendar();
        cal.setTime(startDate);
       
        long days = 0;
        for (long i = 0; i < businessDays; i++) {
            int weekDay = cal.get(Calendar.DAY_OF_WEEK);
            if (weekDay == Calendar.SATURDAY || weekDay == Calendar.SUNDAY) i--;
            cal.add(Calendar.DAY_OF_WEEK,1);
        }
        return cal.getTime();
    }
ASKER CERTIFIED SOLUTION
Avatar of dberner9
dberner9
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial