Java Date Add add(Date date, int field, int amount)

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

Description

Date Arithmetic function.

License

Open Source License

Parameter

Parameter Description
date The date to perform the arithmetic function on
field A Calendar constant to retrieve the field value from the Date object. Same as for #get get() .
amount the amount of date or time to be added to the field

Return

The date as a result of the execution of the arithmetic function.

Declaration

public static Date add(Date date, int field, int amount) 

Method Source Code

//package com.java2s;
/**//from  w  w w .j  ava2  s  .c  o  m
*  Copyright 2012 NCS Pte. Ltd. All Rights Reserved
*
*  This software is confidential and proprietary to NCS Pte. Ltd. You shall
*  use this software only in accordance with the terms of the license
*  agreement you entered into with NCS.  No aspect or part or all of this
*  software may be reproduced, modified or disclosed without full and
*  direct written authorisation from NCS.
*
*  NCS SUPPLIES THIS SOFTWARE ON AN "AS IS" BASIS. NCS MAKES NO
*  REPRESENTATIONS OR WARRANTIES, EITHER EXPRESSLY OR IMPLIEDLY, ABOUT THE
*  SUITABILITY OR NON-INFRINGEMENT OF THE SOFTWARE. NCS SHALL NOT BE LIABLE
*  FOR ANY LOSSES OR DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
*  MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*/

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

public class Main {
    /**
     * Date Arithmetic function. Adds the specified (signed) amount of time to
     * the given time field, based on the calendar's rules.
     * <p>
     * For example, to subtract 5 days from a specific date, it can be achieved
     * by calling: <p>
     * DateUtil.add(date, Calendar.DATE, -5).
     * <p>
     * @param date   The date to perform the arithmetic function on
     * @param field  A Calendar constant to retrieve the field value from the Date
     * object. Same as for {@link #get get()}.
     * @param amount the amount of date or time to be added to the field
     * @return The date as a result of the execution of the arithmetic function.
     */
    public static Date add(Date date, int field, int amount) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(field, amount);

        return cal.getTime();
    }
}

Related

  1. add(Date actual, Integer type, Integer count)
  2. add(Date date, int calendarField, int amount)
  3. add(Date date, int calendarField, int amount)
  4. add(Date date, int calendarField, int amount)
  5. add(Date date, int field, int amount)
  6. add(Date date, int field, int diff)
  7. add(Date date, int field, int value)
  8. add(Date date, int field, long amount)
  9. add(Date date, int x)