Java Calendar Day getWorkingDay(Calendar beginDate, Calendar endDate)

Here you can find the source of getWorkingDay(Calendar beginDate, Calendar endDate)

Description

get Working Day

License

Apache License

Declaration

public static int getWorkingDay(Calendar beginDate, Calendar endDate) 

Method Source Code

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

import java.util.Calendar;

public class Main {

    public static int getWorkingDay(Calendar beginDate, Calendar endDate) {
        int result = -1;
        if (beginDate.after(endDate)) {
            Calendar swap = beginDate;
            beginDate = endDate;/*from ww w  .j  av  a2  s. c  o m*/
            endDate = swap;
        }
        int charge_start_date = 0;
        int charge_end_date = 0;
        int stmp;
        int etmp;
        stmp = 7 - beginDate.get(Calendar.DAY_OF_WEEK);
        etmp = 7 - endDate.get(Calendar.DAY_OF_WEEK);
        if (stmp != 0 && stmp != 6) {
            charge_start_date = stmp - 1;
        }
        if (etmp != 0 && etmp != 6) {
            charge_end_date = etmp - 1;
        }
        result = (getDaysBetween(getNextMonday(beginDate),
                getNextMonday(endDate)) / 7)
                * 5
                + charge_start_date
                - charge_end_date;
        return result;
    }

    public static int getDaysBetween(Calendar beginDate, Calendar endDate) {
        if (beginDate.after(endDate)) {
            Calendar swap = beginDate;
            beginDate = endDate;
            endDate = swap;
        }
        int days = endDate.get(Calendar.DAY_OF_YEAR)
                - beginDate.get(Calendar.DAY_OF_YEAR) + 1;
        int year = endDate.get(Calendar.YEAR);
        if (beginDate.get(Calendar.YEAR) != year) {
            beginDate = (Calendar) beginDate.clone();
            do {
                days += beginDate.getActualMaximum(Calendar.DAY_OF_YEAR);
                beginDate.add(Calendar.YEAR, 1);
            } while (beginDate.get(Calendar.YEAR) != year);
        }
        return days;
    }

    public static Calendar getNextMonday(Calendar date) {
        Calendar result = null;
        result = date;
        do {
            result = (Calendar) result.clone();
            result.add(Calendar.DATE, 1);
        } while (result.get(Calendar.DAY_OF_WEEK) != 2);
        return result;
    }
}

Related

  1. getSafeTimeForAllDay(Calendar cal)
  2. getStartAndEndOfDay(Calendar calendar)
  3. getStartOfDay(Calendar cal)
  4. getThatMonday(Calendar cal)
  5. getTodayCalendar()
  6. getWorkingDay(Calendar d1, Calendar d2)
  7. incrementDay(Calendar cal)
  8. incrementOneDay(Calendar calendar)
  9. isAfterDay(Calendar cal1, Calendar cal2)