private Calendar addMonth(int numMonths) {
    boolean lastDatOfMonth = isLastDayOfMonth(calendar);

    calendar.add(Calendar.MONTH, numMonths);

    if (lastDatOfMonth) {
      int maxDd = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
      calendar.set(Calendar.DAY_OF_MONTH, maxDd);
    }
    return calendar;
  }
GenericUDFAddMonths.

Add a number of months to the date. The time part of the string will be
ignored.
ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFAddMonths.java