Java Day of Week calculateBusinessDays(Date startDate, Date endDate, int firstWeekendDay, int secondWeekendDay)

Here you can find the source of calculateBusinessDays(Date startDate, Date endDate, int firstWeekendDay, int secondWeekendDay)

Description

calculate Business Days

License

Apache License

Declaration

public static int calculateBusinessDays(Date startDate, Date endDate,
            int firstWeekendDay, int secondWeekendDay) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

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

public class Main {
    public static int calculateBusinessDays(Date startDate, Date endDate,
            int firstWeekendDay, int secondWeekendDay) {
        Calendar startCal = Calendar.getInstance();
        startCal.setTime(startDate);/*from w ww.  j  a va  2s .  co m*/

        Calendar endCal = Calendar.getInstance();
        endCal.setTime(endDate);

        int workDays = 0;

        //Return 0 if start and end are the same
        if (startCal.getTimeInMillis() == endCal.getTimeInMillis()) {
            return 0;
        }

        if (startCal.getTimeInMillis() > endCal.getTimeInMillis()) {
            startCal.setTime(endDate);
            endCal.setTime(startDate);
        }

        do {
            startCal.add(Calendar.DAY_OF_MONTH, 1);
            if (startCal.get(Calendar.DAY_OF_WEEK) != firstWeekendDay
                    && startCal.get(Calendar.DAY_OF_WEEK) != secondWeekendDay) {
                workDays++;
            }
        } while (startCal.getTimeInMillis() <= endCal.getTimeInMillis());

        return workDays;
    }
}

Related

  1. Day_Of_Week(String Date, int DayCase)
  2. dayForWeek(String pTime)
  3. dayForWeek(String pTime)
  4. dayForWeek(String weeks)