Java Day End postponeWorkingDay(final Calendar calendar, final int measureUnit, final int amount, final Long[] holidays)

Here you can find the source of postponeWorkingDay(final Calendar calendar, final int measureUnit, final int amount, final Long[] holidays)

Description

postpone Working Day

License

LGPL

Declaration

public static Date postponeWorkingDay(final Calendar calendar, final int measureUnit, final int amount,
            final Long[] holidays) 

Method Source Code

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

import java.util.*;

public class Main {
    /**/*from   ww  w  . j av  a2 s . co m*/
     * Milliseconds=14 *
     */
    public static final int MILLISECOND = Calendar.MILLISECOND;
    /**
     * Seconds=13 *
     */
    public static final int SECOND = Calendar.SECOND;
    /**
     * Minute=12 *
     */
    public static final int MINUTE = Calendar.MINUTE;
    /**
     * Hour=11 *
     */
    public static final int HOUR = Calendar.HOUR_OF_DAY;
    /**
     * 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 Date postponeWorkingDay(final Date date, final int measureUnit, final int amount,
            final Long[] holidays) {
        Date result = date;
        for (int i = 0; i < amount; i++) {
            result = postpone(result, measureUnit, 1);
            if (!isWorkingDay(result, holidays)) {
                result = nextWorkingDay(result, holidays).getTime();
            }
        }
        return result;
    }

    public static Date postponeWorkingDay(final Calendar calendar, final int measureUnit, final int amount,
            final Long[] holidays) {
        Date result = calendar.getTime();
        for (int i = 0; i < amount; i++) {
            postpone(calendar, measureUnit, 1);
            if (!isWorkingDay(calendar, holidays)) {
                nextWorkingDay(calendar, holidays);
            }
        }
        return calendar.getTime();
    }

    public static Date postpone(final Date date, final int measureUnit, final int amount) {
        final Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        return postpone(calendar, measureUnit, amount);
    }

    public static Date postpone(final Calendar calendar, final int measureUnit, final int amount) {

        switch (measureUnit) {
        case MILLISECOND:
            calendar.add(MILLISECOND, amount);
            break;
        case SECOND:
            calendar.add(SECOND, amount);
            break;
        case MINUTE:
            calendar.add(MINUTE, amount);
            break;
        case HOUR:
            calendar.add(HOUR, amount);
            break;
        case DAY:
            calendar.add(DAY, amount);
            break;
        case MONTH:
            calendar.add(MONTH, amount);
            break;
        case YEAR:
            calendar.add(YEAR, amount);
            break;
        }

        return calendar.getTime();
    }

    /**
     * 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;
    }

    /**
     * @param date
     * @return
     */
    public static Calendar nextWorkingDay(final Date date, final Long[] holidays) {
        return nextWorkingDay(date.getTime(), holidays);
    }

    /**
     * @param time
     * @return
     */
    public static Calendar nextWorkingDay(final Long time, final Long[] holidays) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        return nextWorkingDay(calendar, holidays);
    }

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

Related

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