Java Day Add addDays(final Date date, final int days)

Here you can find the source of addDays(final Date date, final int days)

Description

add Days

License

Open Source License

Return

Adds the given number of days to the given date and return a copy.

Declaration

public static Date addDays(final Date date, final int days) 

Method Source Code

//package com.java2s;
/**//w w  w.  j  av  a  2  s.  c  o m
 *
 * Licensed under the GNU General Public License (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.gnu.org/licenses/gpl.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * 
 * @author Vadim Kisen
 *
 * copyright 2010 by uTest 
 */

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Main {
    public static final int DAY_OF_MONTH = Calendar.DAY_OF_MONTH;
    public static final int HOUR = Calendar.HOUR;
    public static final int MINUTE = Calendar.MINUTE;
    public static final int SECOND = Calendar.SECOND;

    /**
     * @return Adds the given number of days to the given date and return a
     *         copy.
     */
    public static Date addDays(final Date date, final int days) {
        final GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(date);
        gc.add(Calendar.DAY_OF_MONTH, days);
        return gc.getTime();
    }

    /**
     * Given a Date as parameter, it sets it to the specified time and returns a
     * copy.
     * 
     * @param date
     *            Original date
     * @param hours
     *            Hours to set to the date
     * @param minutes
     *            Minutes to set to the date
     * @param seconds
     *            Seconds to set to the date
     * 
     * @return A new Date with the given time.
     */
    public static Date setTime(final Date date, final int hours, final int minutes, final int seconds) {
        final Calendar aux = Calendar.getInstance();
        aux.setTime(date);
        aux.set(Calendar.MILLISECOND, 0);
        aux.set(Calendar.HOUR, hours);
        aux.set(Calendar.MINUTE, minutes);
        aux.set(Calendar.SECOND, seconds);
        return aux.getTime();
    }
}

Related

  1. addDays(Date dt, int numDays)
  2. addDays(Date start, int days)
  3. addDays(Date startDate, int numDays)
  4. addDays(Date when, int amount)
  5. addDays(final Date date, final int addDays)
  6. addDays(final Date date, final int days)
  7. addDays(int days)
  8. addDays(int days, Date date)
  9. addDays(Integer dateAsInt, Integer daysToAdd)