Java Day Add addDays(Date date, int amount)

Here you can find the source of addDays(Date date, int amount)

Description

Adds a number of days to a date returning a new object.

License

Open Source License

Parameter

Parameter Description
date the date, not null
amount the amount to add, may be negative

Exception

Parameter Description
IllegalArgumentException if the date is null

Return

the new date object with the amount added

Declaration

public static Date addDays(Date date, int amount) 

Method Source Code

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

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

public class Main {
    /**/*from ww w  .j  a  v  a 2 s .c om*/
     * Adds a number of days to a date returning a new object.
     * The original date object is unchanged.
     *
     * @param date  the date, not null
     * @param amount  the amount to add, may be negative
     * @return the new date object with the amount added
     * @throws IllegalArgumentException if the date is null
     */
    public static Date addDays(Date date, int amount) {
        return add(date, Calendar.DAY_OF_MONTH, amount);
    }

    /**
     * From Apache Lang.
     * Adds to a date returning a new object.
     * The original date object is unchanged.
     *
     * @param date  the date, not null
     * @param calendarField  the calendar field to add to
     * @param amount  the amount to add, may be negative
     * @return the new date object with the amount added
     * @throws IllegalArgumentException if the date is null
     * @deprecated Will become privately scoped in 3.0
     */
    public static Date add(Date date, int calendarField, int amount) {
        if (date == null) {
            throw new IllegalArgumentException("The date must not be null");
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(calendarField, amount);
        return c.getTime();
    }
}

Related

  1. addDays(Date baseDate, int amount, boolean endOfDay)
  2. addDays(Date beginDate, int days)
  3. addDays(Date d, double count)
  4. addDays(Date d, int days)
  5. addDays(Date d, int n)
  6. addDays(Date date, int count)
  7. addDays(Date date, int day)
  8. addDays(Date date, int days)
  9. addDays(Date date, int days)