Java Date Subtract subtractDays(Date dt, int days)

Here you can find the source of subtractDays(Date dt, int days)

Description

Subtract a number of days

License

Open Source License

Parameter

Parameter Description
dt a parameter
days a parameter

Declaration

public static Date subtractDays(Date dt, int days) 

Method Source Code

//package com.java2s;
/*//w  w  w.j  av a 2s  . c  om
 * Author Stephen Booysen
 *
 * Copyright (c) 2015 Stephen Booysen, Inc. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * Stephen Booysen. ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Stephen Booysen
 *
 * Stephen Booysen MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

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

public class Main {
    /**
     * Subtract a number of days
     * 
     * @param dt
     * @param days
     * @return
     */
    public static Date subtractDays(Date dt, int days) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        cal.add(Calendar.DATE, -days);
        return cal.getTime();
    }

    /**
     * Set the time of the given Date
     *
     * @param date
     * @param hourOfDay
     * @param minute
     * @param second
     * @param ms
     *
     * @return new instance of java.util.Date with the time set
     */
    public static Date setTime(final Date date, final int hourOfDay, final int minute, final int second,
            final int ms) {
        final GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(date);
        gc.set(Calendar.HOUR_OF_DAY, hourOfDay);
        gc.set(Calendar.MINUTE, minute);
        gc.set(Calendar.SECOND, second);
        gc.set(Calendar.MILLISECOND, ms);
        return gc.getTime();
    }
}

Related

  1. subtract(Date date, int num)
  2. subtractCertainYears(java.util.Date date, Integer years)
  3. subtractDate(Date date, int type, Integer quantity)
  4. subtractDate(Date date, int type, Integer quantity)
  5. subtractDay(final Date date)
  6. subtractDurationToDate(Date date, String duration)
  7. subtractNowDay(Date date)
  8. subtractOrAddDayDuration(Date date, int duration)