Java Day Add addDays(Date date, int daysOffset, boolean stripTime)

Here you can find the source of addDays(Date date, int daysOffset, boolean stripTime)

Description

Adds specified number of days to date and, optionally strips the time component.

License

Mozilla Public License

Parameter

Parameter Description
date Date value to process.
daysOffset # of days to add.
stripTime If true, strip the time component.

Return

Input value the specified operations applied.

Declaration

public static Date addDays(Date date, int daysOffset, boolean stripTime) 

Method Source Code

//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();

    }
}

Related

  1. addDays(Date date, int days)
  2. addDays(Date date, int days)
  3. addDays(Date date, int days)
  4. addDays(Date date, int days)
  5. addDays(Date date, int days)
  6. addDays(Date date, int daysToAdd)
  7. addDays(Date date, int i)
  8. addDays(Date dt, int numDays)
  9. addDays(Date start, int days)