Android Date Add add(Date date, int calendarField, int amount)

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

Description

Used internally to add/subtract amount to specific calendar field.

Parameter

Parameter Description
date - Date object.
calendarField -calendar specific field.
amount -int, value by which field should be changed.

Exception

Parameter Description
NullPointerException if any of the parameters is null.

Return

-new object with updated time.

Declaration

private static Date add(Date date, int calendarField, int amount) 

Method Source Code

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class Main{
    /**/*from   w  w  w.  j  av  a2  s  . c  o m*/
     * 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();
    }
}

Related

  1. add(Date date, int calendarField, int amount)
  2. add(Date date, int calendarField, int amount)
  3. addDay(Date date, int days)
  4. addDays(Date date, int amount)