Here you can find the source of addDate(Date date, String unit, int quantity)
Parameter | Description |
---|---|
date | start date |
unit | the unit for add |
quantity | the quantity for add |
public static Date addDate(Date date, String unit, int quantity)
//package com.java2s; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; public class Main { /**/* w w w. j av a 2 s .c o m*/ * add date * * @param date start date * @param unit the unit for add * @param quantity the quantity for add * @return date */ public static Date addDate(Date date, String unit, int quantity) { if (unit.equalsIgnoreCase("Year")) { return addYear(date, quantity); } if (unit.equalsIgnoreCase("Month")) { return addMonth(date, quantity); } if (unit.equalsIgnoreCase("Week")) { return addWeek(date, quantity); } if (unit.equalsIgnoreCase("Day")) { return addDay(date, quantity); } if (unit.equalsIgnoreCase("Hour")) { return addHour(date, quantity); } if (unit.equalsIgnoreCase("Minute")) { return addMinute(date, quantity); } if (unit.equalsIgnoreCase("Second")) { return addSecond(date, quantity); } return null; } /** * <pre> * The calculation of the date of the increase or decrease the number of years after the date. * years is positive for Canada, is negative for Less * * DateUtils.addYear(20120101,2) = 20140101 * DateUtils.addYear(20120101,-2) = 20100101 * </pre> * * @param date target date * @param years variable number * @return Date */ public static Date addYear(Date date, int years) { if (date == null) { return null; } Calendar c = getCalendar(date); c.add(Calendar.YEAR, years); return c.getTime(); } /** * <pre> * The calculation of the date of the increase or decrease the number of months after the date. * months is positive for Canada, is negative for Less * * <code> * DateUtils.addMonth(1970-01-01, 1) = 1970-02-01 * </code> * </pre> * * @param date target date * @param months variable number * @return Date */ public static Date addMonth(Date date, int months) { if (date == null) { return null; } Calendar c = getCalendar(date); c.add(Calendar.MONTH, months); return c.getTime(); } /** * <pre> * The calculation of the date of the increase or decrease the number of weeks after the date. * weeks is positive for Canada, is negative for Less * * DateUtils.addWeek(1970-01-01, 1) = 1970-01-08 * </pre> * * @param date target date * @param weeks variable number * @return Date */ public static Date addWeek(Date date, int weeks) { if (date == null) { return null; } Calendar c = getCalendar(date); c.add(Calendar.WEEK_OF_MONTH, weeks); return c.getTime(); } /** * <pre> * The calculation of the date of increase, reducing the number of days of the date. * days is a positive number that when added, is negative for the reduction * * DateUtils.addDay(1970-01-01, 1) = 1970-01-02 * </pre> * * @param date target date * @param days variable number * @return Date */ public static Date addDay(Date date, int days) { if (date == null) { return null; } Calendar c = getCalendar(date); c.add(Calendar.DATE, days); return c.getTime(); } /** * <pre> * The calculation of the date of increase or decrease the number of hours after the date. * hours is positive for Canada, is negative for Less * * DateUtils.addHour(1970-01-01 10:00, 1) = 1970-01-01 11:00 * </pre> * * @param date target date * @param hours variable number * @return Date */ public static Date addHour(Date date, int hours) { if (date == null) { return null; } Calendar c = getCalendar(date); c.add(Calendar.HOUR, hours); return c.getTime(); } /** * <pre> * The calculation of the date of increase or decrease the number of minutes later date. * minutes is positive for Canada, is negative for Less * * DateUtils.addMinute(1970-01-01 10:00, 1) = 1970-01-01 10:01 * </pre> * * @param date target date * @param minutes variable number * @return Date */ public static Date addMinute(Date date, int minutes) { if (date == null) { return null; } Calendar c = getCalendar(date); c.add(Calendar.MINUTE, minutes); return c.getTime(); } /** * <pre> * Increase or reduce the number of seconds after the date of calculation date * when seconds is a positive number is added as a negative number is less. * * DateUtils.addSecond(1970-01-01 10:00:00, 1) = 1970-01-01 10:00:01 * </pre> * * @param date target date * @param seconds variable number * @return Date */ public static Date addSecond(Date date, int seconds) { if (date == null) { return null; } Calendar c = getCalendar(date); c.add(Calendar.SECOND, seconds); return c.getTime(); } /** * get GregorianCalendar. * * @return Calendar,GregorianCalendar. */ public static Calendar getCalendar() { return new GregorianCalendar(); } /** * get GregorianCalendar with given date. * * @param date Date. * @return Calendar, GregorianCalendar. */ public static Calendar getCalendar(Date date) { Calendar calendar = getCalendar(); calendar.setTime(date); return calendar; } }