Java Date Subtract subtractDate(Date date, int type, Integer quantity)

Here you can find the source of subtractDate(Date date, int type, Integer quantity)

Description

subtract Date

License

Open Source License

Parameter

Parameter Description
date A java.util.Date object
type A constant of java.util.Calendar class (Calendar.MINUTE, Calendar.HOUR_OF_DAY, etc...)
quantity The period of time that will be decreased on date

Return

A java.util.Date object

Declaration

public static Date subtractDate(Date date, int type, Integer quantity) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    private static int SUBTRACT = 1;

    /**//from  www.  j  a va 2 s  . c o  m
     * 
     * @param date A java.util.Date object
     * @param type A constant of java.util.Calendar 
     *             class (Calendar.MINUTE, Calendar.HOUR_OF_DAY, etc...)
     * @param quantity The period of time that will be decreased on date
     * @return A java.util.Date object
     */
    public static Date subtractDate(Date date, int type, Integer quantity) {
        return manipulateDate(date, type, quantity, SUBTRACT);
    }

    /**
     * 
     * @param date A java.util.Date object
     * @param type A constant of java.util.Calendar 
     *             class (Calendar.MINUTE, Calendar.HOUR_OF_DAY, etc...)
     * @param quantity The period of time that will be decreased or incresead on date
     * @param operation An integer, if equals 0, sum the date, if different of 0, subtract 
     * @return A java.util.Date object
     */
    private static Date manipulateDate(Date date, int type, Integer quantity, int operation) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);

        if (operation == 0) {
            c.set(type, c.get(type) + quantity);
        } else {
            c.set(type, c.get(type) - quantity);
        }

        return c.getTime();
    }
}

Related

  1. substractDaysFromDate(final Date date, final int substractDays)
  2. subtract(Date date, int num)
  3. subtractCertainYears(java.util.Date date, Integer years)
  4. subtractDate(Date date, int type, Integer quantity)
  5. subtractDay(final Date date)
  6. subtractDays(Date dt, int days)
  7. subtractDurationToDate(Date date, String duration)