Java Date Add dateAddMonths(Date date, int addMonths)

Here you can find the source of dateAddMonths(Date date, int addMonths)

Description

date Add Months

License

Apache License

Declaration

public static Date dateAddMonths(Date date, int addMonths) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

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

public class Main {
    public static Date dateAddMonths(Date date, int addMonths) {
        if (date == null)
            return null;
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);/*from w ww .  jav  a 2  s.c  om*/
        cal.add(Calendar.MONTH, addMonths);
        return cal.getTime();
    }

    public static Date dateAddMonths(Date date, double months) {
        if (date == null)
            return null;
        int addMonths = (int) Math.ceil(months);
        int addDays = (int) Math.round((months - addMonths) * 31);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, addMonths);
        cal.add(Calendar.DATE, addDays);
        return cal.getTime();
    }
}

Related

  1. dateAddDays(Date date, int days)
  2. dateAddDays(Date time, int days, String dateFormat)
  3. dateAddHours(Date time, int hours, SimpleDateFormat dateFormat)
  4. dateAddMinutes(Date date, int minutes)
  5. dateAddMM(Date date, int i)
  6. dateAddSpaceMilliSecond(Date date, int milliSecond)