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

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;

    public static Date subtractDate(Date date, int type, Integer quantity) {
        return manipulateDate(date, type, quantity, SUBTRACT);
    }//  ww  w . j  a v a  2 s. c  o  m

    private static Date manipulateDate(Date date, int type, Integer quantity, int operation) {
        if (date == null) {
            throw new IllegalArgumentException("Sum/subtraction date should not be null");
        }

        if (quantity < 0) {
            throw new IllegalArgumentException("Sum/subtraction should not receive a negative value");
        }

        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)
  8. subtractNowDay(Date date)