Here you can find the source of add(Date inputDate, int interval, int calendarUnit)
Parameter | Description |
---|---|
inputDate | Date to Add |
interval | Interval to added. Subtract is interval is nagative |
calendarUnit | Calendar.Year/Calendar.Month/Calendar.Day/Calendar.Hour/Calendar.MInute |
public static Date add(Date inputDate, int interval, int calendarUnit)
//package com.java2s; /*//from www . j a va 2 s . co m * File: $RCSfile$ * * Copyright (c) 2005 Wincor Nixdorf International GmbH, * Heinz-Nixdorf-Ring 1, 33106 Paderborn, Germany * All Rights Reserved. * * This software is the confidential and proprietary information * of Wincor Nixdorf ("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 Wincor Nixdorf. */ import java.util.Date; import java.util.Calendar; public class Main { /** * Add Interval to Date * * @param inputDate Date to Add * @param interval Interval to added. Subtract is interval is nagative * @param calendarUnit Calendar.Year/Calendar.Month/Calendar.Day/Calendar.Hour/Calendar.MInute * @return Date added */ public static Date add(Date inputDate, int interval, int calendarUnit) { Calendar cal = Calendar.getInstance(); cal.setTime(formateDateWithoutTime(inputDate)); cal.add(calendarUnit, interval); return cal.getTime(); } /** * Format Date to make it''s hour/minute/second to 0 * * @param inputDate Date to Format * @return Date with Year/Month/Day is same with input one while hour/minute/second is 0 */ public static Date formateDateWithoutTime(Date inputDate) { Date outputDate = inputDate; Calendar cal = Calendar.getInstance(); cal.setTime(outputDate); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); outputDate = cal.getTime(); return outputDate; } }