Java Date Adjust adjustDate(Date dtDate, int iYears, int iMonths, int iDays, int iHours, int iMinutes, int iSeconds)

Here you can find the source of adjustDate(Date dtDate, int iYears, int iMonths, int iDays, int iHours, int iMinutes, int iSeconds)

Description

Adjusts the Date passed in by the appropriate amounts.

License

Open Source License

Parameter

Parameter Description
dtDate a parameter
iYears a parameter
iMonths a parameter
iDays a parameter
iHours a parameter
iMinutes a parameter
iSeconds a parameter

Declaration

public static java.util.Date adjustDate(Date dtDate, int iYears, int iMonths, int iDays, int iHours,
        int iMinutes, int iSeconds) 

Method Source Code

//package com.java2s;
/** ***************************************************************
Util.java//from w  ww  .  j  a  v  a  2  s .c  o  m
Copyright (C) 2001  Brendon Upson 
http://www.wnc.net.au info@wnc.net.au
    
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
    
 *************************************************************** */

import java.util.Calendar;

import java.util.Date;

public class Main {
    /**
     * Adjusts the Date passed in by the appropriate amounts.
     * @param dtDate
     * @param iYears
     * @param iMonths
     * @param iDays
     * @param iHours
     * @param iMinutes
     * @param iSeconds
     * @return
     */
    public static java.util.Date adjustDate(Date dtDate, int iYears, int iMonths, int iDays, int iHours,
            int iMinutes, int iSeconds) {
        if (dtDate == null)
            return null;
        Calendar cal = Calendar.getInstance();
        Calendar calNew = Calendar.getInstance();
        cal.setTime(dtDate);
        calNew.setTime(dtDate);//to ensure milliseconds are copied
        calNew.set(cal.get(Calendar.YEAR) + iYears, cal.get(Calendar.MONTH) + iMonths,
                cal.get(Calendar.DAY_OF_MONTH) + iDays, cal.get(Calendar.HOUR_OF_DAY) + iHours,
                cal.get(Calendar.MINUTE) + iMinutes, cal.get(Calendar.SECOND) + iSeconds);
        //dtAdjusted = new java.util.Date(calNew.getTime().getTime());
        //return dtAdjusted;
        return calNew.getTime();
    }
}

Related

  1. adjustDays(Date startingDate, int deltaDays)
  2. adjustHour(Date dt, int hour)
  3. adjustHourAndMinute(Date n)
  4. adjustTime(Date date, int hour, int minute, int second)