Here you can find the source of addMonths(int updateInterval, String dateFormat)
public static String addMonths(int updateInterval, String dateFormat)
//package com.java2s; import java.util.*; public class Main { /** //from w w w. j a va 2 s. c o m * Add months to todays date and return as string */ public static String addMonths(int updateInterval, String dateFormat) { if (updateInterval == -1) return "Exempt"; SimpleDateFormat timeFormat = new SimpleDateFormat(dateFormat); Calendar tCalendar = Calendar.getInstance(); Date tDate = new Date(); Integer year = Integer.parseInt(new SimpleDateFormat(separateDate(dateFormat, "y")).format(tDate)); // Isolate year / month / day Integer month = Integer.parseInt(new SimpleDateFormat(separateDate(dateFormat, "M")).format(tDate)) - 1; Integer day = Integer.parseInt(new SimpleDateFormat(separateDate(dateFormat, "d")).format(tDate)); // Disaster recovery? tCalendar.set(year, month, day); tCalendar.add(tCalendar.MONTH, updateInterval); return timeFormat.format(tCalendar.getTime()); } /** * Quick hack to isolate 'dd', 'yyyy', 'MM', etc */ private static String separateDate(String dateFormat, String pattern) { for (String i : new String("- / M d y \\ | ( ) ! @ # $ % ^ & * : ; < > ?").split(" ")) { if (!i.equals(pattern)) dateFormat = dateFormat.replace(i, ""); } return dateFormat; } }