Here you can find the source of addDaysToDate(Date date, int daysToBeAdded)
Parameter | Description |
---|---|
date | - Date object. |
daysToBeAdded | -amount by which to be changed. |
Parameter | Description |
---|---|
NullPointerException | if any of the parameters is null. |
public static Date addDaysToDate(Date date, int daysToBeAdded)
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; public class Main{ /***/* ww w. jav a 2 s. c o m*/ * Add / Subtract given no of days to {@link Date}. * * @param date -{@link Date} object. * @param daysToBeAdded -amount by which to be changed. * @return -new {@link Date} object with updated time. * @throws NullPointerException if any of the parameters is null. */ public static Date addDaysToDate(Date date, int daysToBeAdded) { if (StringUtils.isNull(date)) { throw new NullPointerException("date cannot be null"); } return add(date, Calendar.DATE, daysToBeAdded); } /** * Used internally to add/subtract amount to specific calendar field. * * @param date -{@link Date} object. * @param calendarField -calendar specific field. * @param amount -int, value by which field should be changed. * @return -new {@link Date} object with updated time. * @throws NullPointerException if any of the parameters is null. */ private static Date add(Date date, int calendarField, int amount) { if (StringUtils.isNull(date)) { throw new NullPointerException("date cannot be null"); } Calendar c = Calendar.getInstance(); c.setTime(date); c.add(calendarField, amount); return c.getTime(); } }