Java Week Add addWeekDays(Date dt, int days)

Here you can find the source of addWeekDays(Date dt, int days)

Description

Add number of days to current Date and return the new Date.

License

Open Source License

Parameter

Parameter Description
dt The given date
days The number of days to add

Return

date The new Date

Declaration

public static Date addWeekDays(Date dt, int days) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Main {
    /**//from   ww w .  j  a  v  a  2s. c om
     * Add number of days to current Date and return the new Date.
     * 
     * @param dt
     *            The given date
     * @param days
     *            The number of days to add
     * 
     * @return date The new Date
     */
    public static Date addWeekDays(Date dt, int days) {
        if (isWeekend(dt)) {
            dt = nextMonday(dt);
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);

        if (days > 0) {
            for (int j = 1; j <= days; j++) {
                if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) {
                    cal.setTime(nextMonday(cal.getTime()));
                } else {
                    cal.add(Calendar.DATE, 1);
                }
            }
        } else if (days < 0) {
            for (int j = days; j <= -1; j++) {
                if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
                    cal.add(Calendar.DATE, -3);
                } else {
                    cal.add(Calendar.DATE, -1);
                }
            }
        }
        return cal.getTime();
    }

    /**
     * Check whether a date is a weekend
     * 
     * @param dt
     *            The given Date
     * 
     * @return boolean true if weekend, else false
     */
    public static boolean isWeekend(Date dt) {
        return isSaturday(dt) || isSunday(dt);
    }

    /**
     * Return next monday
     * 
     * @param dt
     * @return
     */
    public static Date nextMonday(Date dt) {
        Calendar cal = new GregorianCalendar();
        cal.setTime(dt);
        int days = cal.get(Calendar.DAY_OF_WEEK) - Calendar.MONDAY;
        if (days < 0) {
            cal.add(Calendar.DATE, (-days));
        } else {
            cal.add(Calendar.DATE, (7 - days));
        }
        return cal.getTime();
    }

    /**
     * Check whether a date is Saturday
     * 
     * @param dt
     *            The given Date
     * 
     * @return boolean true if Saturday, else false
     */
    public static boolean isSaturday(Date dt) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int day = cal.get(Calendar.DAY_OF_WEEK);

        return (day == Calendar.SATURDAY) ? true : false;
    }

    /**
     * Check whether a date is Sunday
     * 
     * @param dt
     *            The given Date
     * 
     * @return boolean true if Sunday, else false
     */
    public static boolean isSunday(Date dt) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int day = cal.get(Calendar.DAY_OF_WEEK);

        return (day == Calendar.SUNDAY) ? true : false;
    }
}

Related

  1. addWeeksToDate(long timeInMilis, int amount)