Java Month Add addMonths(int updateInterval, String dateFormat)

Here you can find the source of addMonths(int updateInterval, String dateFormat)

Description

Add months to todays date and return as string

License

GNU General Public License

Declaration

public static String addMonths(int updateInterval, String dateFormat) 

Method Source Code

//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;
    }
}

Related

  1. addMonth(Date date, int month)
  2. addMonth(Date date, int month)
  3. addMonth(Date date, int months)
  4. addMonths(Date date, int iMonthCount)
  5. addMonths(Date date, int months)
  6. addMonths(java.util.Date today, int monthsToAdd)
  7. addMonths(String s, int month)
  8. addMonths2(String dateStr, int months)
  9. addMonthsAndDays(Date date, int months, int days)