Java Time Add addTime(Date baseDate, int amount, int datePart)

Here you can find the source of addTime(Date baseDate, int amount, int datePart)

Description

Adds the specified part of date or time to the provided date instance.

License

Open Source License

Parameter

Parameter Description
baseDate The date to add the specified part of the date.
amount Amount to add.
datePart Part of the date to add. See Calendar

Return

the new date

Declaration

public static Date addTime(Date baseDate, int amount, int datePart) 

Method Source Code

//package com.java2s;
/*//  w w w  .  j a  v  a2  s.co  m
 * $Id: DateUtils.java,v 1.4 2005/10/10 18:02:45 rbair Exp $
 *
 * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
 * Santa Clara, California 95054, U.S.A. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

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

public class Main {
    /**
     * Adds the specified part of  date or time to the provided date instance.
     *
     * @param baseDate The date to add the specified part of the date.
     * @param amount Amount to add.
     * @param datePart Part of the date to add. See {@link Calendar}
     * @return the new date
     */
    public static Date addTime(Date baseDate, int amount, int datePart) {
        Calendar cal = Calendar.getInstance();
        synchronized (cal) {
            cal.setTime(baseDate);
            cal.add(datePart, amount);
        }
        return cal.getTime();
    }
}

Related

  1. addTime(Date aDate, int timeToAdd, int timeUnits)
  2. addTime(Date aDate, int timeToAdd, int timeUnits)
  3. addTime(Date current, Date toadd)
  4. addTime(Date date, Date addTime)
  5. addTime(Date date, int field, int value)
  6. addTime(Date date, int field, int value)