Java Date Subtract subtractOrAddDayDuration(Date date, int duration)

Here you can find the source of subtractOrAddDayDuration(Date date, int duration)

Description

subtract Or Add Day Duration

License

Open Source License

Parameter

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

Declaration

public static Date subtractOrAddDayDuration(Date date, int duration) 

Method Source Code

//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;
    }
}

Related

  1. subtractDate(Date date, int type, Integer quantity)
  2. subtractDay(final Date date)
  3. subtractDays(Date dt, int days)
  4. subtractDurationToDate(Date date, String duration)
  5. subtractNowDay(Date date)