Here you can find the source of addTime(Date baseDate, int amount, int datePart)
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 |
public static Date addTime(Date baseDate, int amount, int datePart)
//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(); } }