John C John C - 1 year ago 104
Java Question

Calculate number of weekdays between two dates in Java

Can anyone point me to some Java snippet wherein i can get business (except Sat and Sun) days between two dates.

Answer Source
public static int getWorkingDaysBetweenTwoDates(Date startDate, Date endDate) {
    Calendar startCal = Calendar.getInstance();

    Calendar endCal = Calendar.getInstance();

    int workDays = 0;

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

    if (startCal.getTimeInMillis() > endCal.getTimeInMillis()) {

    do {
       //excluding start date
        startCal.add(Calendar.DAY_OF_MONTH, 1);
        if (startCal.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY && startCal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
    } while (startCal.getTimeInMillis() < endCal.getTimeInMillis()); //excluding end date

    return workDays;

Start date and end date are exclusive, Only the days between given dates will be counted. Start date and end date will not be included.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download