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;
/*// ww  w  .ja  va  2 s  .  c o m
 * CONFIDENTIAL AND PROPRIETARY SOURCE CODE OF
 * Institute of Systems Science, National University of Singapore
 *
 * Copyright 2012 Team 4(Part-Time), ISS, NUS, Singapore. All rights reserved.
 * Use of this source code is subjected to the terms of the applicable license
 * agreement.
 *
 * -----------------------------------------------------------------
 * REVISION HISTORY
 * -----------------------------------------------------------------
 * DATE             AUTHOR          REVISION      DESCRIPTION
 * 10 March 2012    Chen Changfeng   0.1            Class creating
 *                                        
 *                                        
 *                                        
 *                                        
 * 
 */

import java.util.*;

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)