Java Timestamp addMonths(Timestamp refDate, int nrOfMonthsToAdd)

Here you can find the source of addMonths(Timestamp refDate, int nrOfMonthsToAdd)

Description

Adds the number of months to the date.

License

Mozilla Public License

Parameter

Parameter Description
refDate the original date
nrOfMonthsToAdd the number of months to add

Return

the new timestamp

Declaration

public static Timestamp addMonths(Timestamp refDate, int nrOfMonthsToAdd) 

Method Source Code

//package com.java2s;
/*//from   www  .java 2s. co m
 * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, and the
 * EPL 1.0 (http://h2database.com/html/license.html). Initial Developer: H2
 * Group Iso8601: Initial Developer: Robert Rathsack (firstName dot lastName at
 * gmx dot de)
 */

import java.sql.Timestamp;

import java.util.Calendar;

public class Main {
    /**
     * Adds the number of months to the date. If the resulting month's number of
     * days is less than the original's day-of-month, the resulting
     * day-of-months gets adjusted accordingly: <br>
     * 30.04.2007 - 2 months = 28.02.2007
     *
     * @param refDate the original date
     * @param nrOfMonthsToAdd the number of months to add
     * @return the new timestamp
     */
    public static Timestamp addMonths(Timestamp refDate, int nrOfMonthsToAdd) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(refDate);
        calendar.add(Calendar.MONTH, nrOfMonthsToAdd);

        Timestamp resultDate = new Timestamp(calendar.getTimeInMillis());
        resultDate.setNanos(refDate.getNanos());
        return resultDate;
    }
}

Related

  1. addDaysToTimestamp(Timestamp timestamp, int numDays)
  2. addHours(Timestamp date, int numOfHours)
  3. addHoursToTimestamp(Timestamp start, int hours)
  4. addMilli(Timestamp nowDate, int period)
  5. addMonth(java.sql.Timestamp src, int val)
  6. addTime(java.sql.Timestamp start, int year, int month, int day)
  7. adjustTimestamp(Timestamp stamp, int adjType, int adjQuantity, TimeZone timeZone, Locale locale)
  8. adjustTimestamp(Timestamp stamp, Integer adjType, Integer adjQuantity)
  9. adjustTimestamp(Timestamp timestamp, String timezone, int gmtOffset)