Java Day of Week isWeekend(Date date)

Here you can find the source of isWeekend(Date date)

Description

Tests the input date to check if the date falls on a weekend.

License

Open Source License

Parameter

Parameter Description
date The date instance.

Return

True if the date falls on a weekend; False otherwise.

Declaration

public static boolean isWeekend(Date date) 

Method Source Code

//package com.java2s;
/*/*from w  w w .j a  v  a2  s  . c om*/
 * CONFIDENTIAL AND PROPRIETARY SOURCE CODE OF
 * Institute of Systems Science, National University of Singapore
 *
 * Copyright 2012 Team 4(Part-Time), ISS, NUS, Singapore. All rights reserved.
 * Use of this source code is subjected to the terms of the applicable license
 * agreement.
 *
 * -----------------------------------------------------------------
 * REVISION HISTORY
 * -----------------------------------------------------------------
 * DATE             AUTHOR          REVISION      DESCRIPTION
 * 10 March 2012    Chen Changfeng   0.1            Class creating
 *                                        
 *                                        
 *                                        
 *                                        
 * 
 */

import java.util.*;

public class Main {
    /**
     * Tests the input date to check if the date falls on a weekend.
     * Weekend falls on a Saturday or a Sunday.
     * <p>
     * @param date The date instance.
     * @return True if the date falls on a weekend; False otherwise.
     */
    public static boolean isWeekend(Date date) {

        if (isSaturday(date) || isSunday(date))
            return true;
        else
            return false;
    }

    /**
     * Tests the input date to check if the date falls on a Saturday.
     * <p>
     * @param  date The date instance.
     * @return True if the date falls on a Saturday; False otherwise
     */
    public static boolean isSaturday(Date date) {

        if (date == null)
            return false;

        if (get(date, Calendar.DAY_OF_WEEK) == Calendar.SATURDAY)
            return true;
        else
            return false;
    }

    /**
     * Tests the input date to check if the date falls on a Sunday.
     * <p>
     * @param  date The date instance.
     * @return True if the date falls on a Sunday; False otherwise
     */
    public static boolean isSunday(Date date) {

        if (date == null)
            return false;

        if (get(date, Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
            return true;
        else
            return false;
    }

    /**
     * Retrieves the value of the field in the Date.
     * Some common fields that is likely to be used include :
     * <p>
     * <li>Calendar.YEAR - retrieves the year value
     * <li>Calendar.MONTH - retrieves the month value ( 1 - 12 )
     * <li>Calendar.DAY_OF_MONTH - retrieve the day value ( 1 - 31 )
     * <li>Calendar.HOUR - retrieves the hours value in 12 hour format ( 1 - 12 )
     * <li>Calendar.HOUR_OF_DAY - retrieves the hours value in 24 hour format ( 0 - 23 )
     * <li>Calendar.MINUTE - retrieves the minutes value ( 0 - 59 )
     * <li>Calendar.AM_PM - retrieves the am/pm value ( 0 = am; 1 = pm )
     * <p>
     * @param  date  The Date object to extract value from.
     * @param  field A Calendar constant to retrieve the field value from the Date
     * object.
     * @return The value of the field that is requested.
     * @throws ArrayIndexOutOfBoundsException - if specified field is out of
     * range (<code>field</code> &lt; 0 || <code>field</code> &gt;= <code>Calendar.FIELD_COUNT</code>).
     * @see java.util.Calendar
     */
    public static int get(Date date, int field) {

        Calendar cal = Calendar.getInstance();

        cal.setTime(date);

        int value = cal.get(field);

        // Add 1 if the field is Calendar.MONTH since Calendar returns
        // the month value starting from 0.
        if (Calendar.MONTH == field)
            value += 1;

        // If it is 12 am/pm, the value will be 0. Need to change it to 12 for ease of display.
        if (Calendar.HOUR == field && value == 0)
            value = 12;

        return value;
    }
}

Related

  1. isWeekday(Date startDate)
  2. isWeekDay(final Date date)
  3. isWeekEnd(Date date)
  4. isWeekend(Date date)
  5. isWeekend(Date date)
  6. isWeekend(Date date)
  7. isWeekend(Date date)
  8. isWeekend(Date dt)
  9. isWeekEnd(Date in)