Here you can find the source of addDays(Date date, int daysOffset, boolean stripTime)
Parameter | Description |
---|---|
date | Date value to process. |
daysOffset | # of days to add. |
stripTime | If true, strip the time component. |
public static Date addDays(Date date, int daysOffset, boolean stripTime)
//package com.java2s; /**//from w ww . j a v a 2s . c o m * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. * If a copy of the MPL was not distributed with this file, You can obtain one at * http://mozilla.org/MPL/2.0/. * * This Source Code Form is also subject to the terms of the Health-Related Additional * Disclaimer of Warranty and Limitation of Liability available at * http://www.carewebframework.org/licensing/disclaimer. */ import java.util.Calendar; import java.util.Date; public class Main { /** * Adds specified number of days to date and, optionally strips the time component. * * @param date Date value to process. * @param daysOffset # of days to add. * @param stripTime If true, strip the time component. * @return Input value the specified operations applied. */ public static Date addDays(Date date, int daysOffset, boolean stripTime) { if (date == null) { return null; } Calendar calendar = Calendar.getInstance(); calendar.setLenient(false); // Make sure the calendar will not perform // automatic correction. calendar.setTime(date); // Set the time of the calendar to the given // date. if (stripTime) { // Remove the hours, minutes, seconds and milliseconds. calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); } calendar.add(Calendar.DAY_OF_MONTH, daysOffset); return calendar.getTime(); } }