Java Day of Work getWorkDay(Date d1, Date d2, int[] freeDays)

Here you can find the source of getWorkDay(Date d1, Date d2, int[] freeDays)

Description

get Work Day

License

Apache License

Declaration

public static int getWorkDay(Date d1, Date d2, int[] freeDays) 

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 getWorkDay(Date d1, Date d2, int[] freeDays) {
        int dNum = 0;
        dNum = (int) ((d2.getTime() - d1.getTime()) / 1000 / 60 / 60 / 24) + 1;

        return dNum - getFreeDay(d1, dNum, freeDays);
    }//from   w  w w .ja  v  a2  s. c  o m

    public static int getWorkDay(Date d1, Date d2) {
        int[] freeDays = { 0, 6 };//default: Sunday and Saturday are the free days.
        return getWorkDay(d1, d2, freeDays);
    }

    public static int getFreeDay(Date date, int dNum, int[] freeDays) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int start = cal.get(Calendar.DAY_OF_WEEK) - 1;
        int freeNum = 0;
        for (int i = 0; i < dNum; i++) {
            for (int j = 0; j < freeDays.length; j++) {
                if ((start + i) % 7 == freeDays[j]) {
                    freeNum++;
                }
            }
        }
        return freeNum;
    }

    public static int getFreeDay(Date date, int dNum) {
        int[] freeDays = { 0, 6 };//default: Sunday and Saturday are the free days.
        return getFreeDay(date, dNum, freeDays);
    }
}

Related

  1. addWorkdays(Date date, int workdayOffset)
  2. addWorkingDays(Date start, int workingDays)
  3. getRemainingWorkingMonth(Date taxdate)
  4. isWorkDay(Date data)
  5. isWorkDay(Date date)
  6. isWorkday(Date date)
  7. isWorking(final Date date)