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,
vibhavAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

StillUnAwareCommented:
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
vibhavAuthor Commented:
can i get a complete code please...
dberner9Commented:
That will only work on days in the same year...
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

dberner9Commented:
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;
    }
dberner9Commented:
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.
vibhavAuthor Commented:
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.
dberner9Commented:
   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();
    }
dberner9Commented:
   private long getEndDate(Date startDate, long businessDays) {
        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();
    }

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.