Java Day End previousWorkingDay(final Calendar calendar, final Long[] holidays)

Here you can find the source of previousWorkingDay(final Calendar calendar, final Long[] holidays)

Description

previous Working Day

License

LGPL

Parameter

Parameter Description
calendar a parameter
holidays (Optional) Array of holidays

Declaration

public static Calendar previousWorkingDay(final Calendar calendar, final Long[] holidays) 

Method Source Code

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

import java.util.*;

public class Main {
    /**/*from   www  . j  ava2  s.c om*/
     * Day=5 *
     */
    public static final int DAY = Calendar.DAY_OF_MONTH;
    /**
     * MONTH=2 *
     */
    public static final int MONTH = Calendar.MONTH;
    /**
     * Year=1 *
     */
    public static final int YEAR = Calendar.YEAR;
    /**
     * no-working days
     */
    private static final int[] highDays = { Calendar.SUNDAY, Calendar.SATURDAY };

    public static Calendar previousWorkingDay(final Date date, final Long[] holidays) {
        return previousWorkingDay(date.getTime(), holidays);
    }

    public static Calendar previousWorkingDay(final Long time, final Long[] holidays) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        // move forward calendar of one day
        calendar.add(DAY, -1);
        while (!isWorkingDay(calendar.getTimeInMillis(), holidays)) {
            calendar.add(DAY, -1);
        }
        return calendar;
    }

    /**
     * @param calendar
     * @param holidays (Optional) Array of holidays
     * @return
     */
    public static Calendar previousWorkingDay(final Calendar calendar, final Long[] holidays) {
        // move backward calendar of one day
        calendar.add(DAY, -1);
        while (!isWorkingDay(calendar.getTimeInMillis(), holidays)) {
            calendar.add(DAY, -1);
        }
        return calendar;
    }

    /**
     * Return true if the day is a working day
     *
     * @param date     Date
     * @param holidays Array of holidays
     * @return Return true if the day is a working day
     */
    public static boolean isWorkingDay(final Date date, final Long[] holidays) {
        return isWorkingDay(date.getTime(), holidays);
    }

    /**
     * Return true if the day is a working day
     *
     * @param time     Long - time in milliseconds
     * @param holidays Array of holidays
     * @return Return true if the day is a working day
     */
    public static boolean isWorkingDay(final long time, final Long[] holidays) {
        final Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);

        return isWorkingDay(calendar, holidays);
    }

    public static boolean isWorkingDay(final Calendar calendar, final Long[] holidays) {

        // check if in holiday
        if (null != holidays) {
            final Calendar holiday = Calendar.getInstance();
            for (final long d : holidays) {
                holiday.setTimeInMillis(d);
                if (holiday.get(Calendar.YEAR) == calendar.get(Calendar.YEAR)
                        && holiday.get(Calendar.MONTH) == calendar.get(Calendar.MONTH)
                        && holiday.get(Calendar.DAY_OF_MONTH) == calendar.get(Calendar.DAY_OF_MONTH)) {
                    return false;
                }
            }
        }

        // check day of week
        final int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        for (final int i : highDays) {
            if (i == dayOfWeek) {
                return false;
            }
        }

        return true;
    }
}

Related

  1. isSameDay(final Calendar calendar1, final Calendar calendar2)
  2. isTodayWithinPeriod(Date periodStart, Date periodEnd)
  3. isValidSendDate(Date sendDate)
  4. isWorkingDay(final Calendar calendar, final Long[] holidays)
  5. postponeWorkingDay(final Calendar calendar, final int measureUnit, final int amount, final Long[] holidays)
  6. secondsBetween(Date start, Date end, boolean assumeSameDate, boolean assumeSameHour)
  7. secondsToEndOfDay(Date aDate)
  8. secondsToEndOfDay(Date aDate)
  9. setEndDay(Date date)