Here you can find the source of subtractOrAddDayDuration(Date date, int duration)
Parameter | Description |
---|---|
date | a parameter |
duration | Example: Date date = subtractOrAddDayDuration(new Date(), -2); // Result: the last 2 days Date date = subtractOrAddDayDuration(new Date(), 2); // Result: the next 2 days |
public static Date subtractOrAddDayDuration(Date date, int duration)
//package com.java2s; /**//from w w w.j a va2 s .c om * This file is part of mycollab-core. * * mycollab-core 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. * * mycollab-core 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 mycollab-core. If not, see <http://www.gnu.org/licenses/>. */ import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; public class Main { /** * * @param date * @param duration * Example: Date date = subtractOrAddDayDuration(new Date(), -2); * // Result: the last 2 days * * Date date = subtractOrAddDayDuration(new Date(), 2); // * Result: the next 2 days * @return */ public static Date subtractOrAddDayDuration(Date date, int duration) { Calendar cal = new GregorianCalendar(); cal.setTime(date); cal.add(Calendar.DAY_OF_MONTH, duration); Date dateExpect = cal.getTime(); return dateExpect; } }