Java Date Add dateAdd(int datePart, int amount, Date date)

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

Description

Method that returns the result of date + amount for the specified datePart using Calendar.get(int) for obtaining it

License

Open Source License

Parameter

Parameter Description
datePart the datePart to use in the calculation
amount amount to add to date
date date for adding amount to

Return

the result from date + amount

Declaration

public static Date dateAdd(int datePart, int amount, Date date) 

Method Source Code

//package com.java2s;
/*/*from w  w w.  j  a  v  a 2  s  .c  o m*/
Copyright 2012 Juan Mart?n Runge
    
jmrunge@gmail.com
http://www.zirsi.com.ar
    
This file is part of ZirUtils.
    
ZirUtils is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
ZirUtils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with ZirUtils.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.util.Calendar;

import java.util.Date;

public class Main {
    /**
     * Method that returns the result of date + amount for the specified datePart
     * using Calendar.get(int) for obtaining it
     * 
     * @param datePart the datePart to use in the calculation
     * @param amount  amount to add to date
     * @param date date for adding amount to
     * @return the result from date + amount
     * @see java.util.Calendar
     */
    public static Date dateAdd(int datePart, int amount, Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(datePart, cal.get(datePart) + amount);
        return cal.getTime();
    }
}

Related

  1. dateAdd(Date date, int days)
  2. dateAdd(Date date, int field, int amount)
  3. dateAdd(Date date, int type, int num)
  4. dateAdd(Date date, int val)
  5. dateAdd(final Date baseDateTime, int daysToAdd, boolean chopTime)
  6. dateAdd(int interval, Date date, int n)
  7. DateAdd(String strDate, int iCount, int iType)
  8. DateAdd3Days(String _date)
  9. dateAddDay(Date date, int day)