Java Week Day calculateFloatingHoliday(int nth, int dayOfWeek, int year, int month)

Here you can find the source of calculateFloatingHoliday(int nth, int dayOfWeek, int year, int month)

Description

This method will take in the various parameters and return a Date object that represents that value.

License

Open Source License

Parameter

Parameter Description
nth 0 for Last, 1 for 1st, 2 for 2nd, etc.
dayOfWeek Use Calendar.MONDAY, Calendar.TUESDAY, etc.
year year
month Use Calendar.JANUARY, etc.

Return

date which corresponds to inputs

Declaration

private static Date calculateFloatingHoliday(int nth, int dayOfWeek, int year, int month) 

Method Source Code


//package com.java2s;

import java.util.*;

public class Main {
    /**//from  www .  j  a  v a2  s.c  om
     * This method will take in the various parameters and return a Date object
     * that represents that value.
     *
     * Ex. To get Martin Luther Kings BDay, which is the 3rd Monday of January,
     * the method call would be:
     *
     * calculateFloatingHoliday(3, Calendar.MONDAY, year, Calendar.JANUARY);
     *
     * Reference material can be found at:
     * http://michaelthompson.org/technikos/holidays.php#MemorialDay
     *
     * @param nth       0 for Last, 1 for 1st, 2 for 2nd, etc.
     * @param dayOfWeek Use Calendar.MONDAY, Calendar.TUESDAY, etc.
     * @param year      year
     * @param month     Use Calendar.JANUARY, etc.
     * @return date which corresponds to inputs
     */
    private static Date calculateFloatingHoliday(int nth, int dayOfWeek, int year, int month) {
        Calendar baseCal = Calendar.getInstance();
        baseCal.clear();
        // Determine what the very earliest day this could occur.
        // If the value was 0 for the nth parameter, increment to the following
        // month so that it can be subtracted after.
        baseCal.set(year, month, 1);
        if (nth <= 0)
            baseCal.add(Calendar.MONTH, 1);
        Date baseDate = baseCal.getTime();

        // Figure out which day of the week that this "earliest" could occur on
        // and then determine what the offset is for our day that we actually
        // need.
        int baseDayOfWeek = baseCal.get(Calendar.DAY_OF_WEEK);
        int fwd = dayOfWeek - baseDayOfWeek;

        // Based on the offset and the nth parameter, we are able to determine
        // the offset of days and then
        // adjust our base date.
        return addDays(baseDate, (fwd + (nth - (fwd >= 0 ? 1 : 0)) * 7));
    }

    /**
     * Add given number of days to a date.
     *
     * @param dateToAdd  The date
     * @param numberOfDay The number of days to add
     * @return new date
     */
    private static Date addDays(Date dateToAdd, int numberOfDay) {
        if (dateToAdd == null) {
            throw new IllegalArgumentException("Date can't be null!");
        }
        Calendar tempCal = Calendar.getInstance();
        tempCal.setTime(dateToAdd);
        tempCal.add(Calendar.DATE, numberOfDay);
        return tempCal.getTime();
    }
}

Related

  1. addMonthToFirstDayOfWeek(Calendar c, int dayOfWeek, int monthInterval)
  2. dayOfWeek()
  3. dayOfWeek(Date pWallDate)
  4. dayOfWeek(final Date date)
  5. dayOfWeek(String pFormerStr)