Java Month Add addMonth(Date date, int months)

Here you can find the source of addMonth(Date date, int months)

Description

add Month

License

Open Source License

Declaration

public static final Date addMonth(Date date, int months) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Main {
    private static String defaultDatePattern = "yyyy-MM-dd";
    public static SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");

    public static final Date addMonth(Date date, int months) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);//from   w ww  .  j av  a  2 s.  c om
        cal.add(Calendar.MONTH, months);
        return cal.getTime();
    }

    public static final String addMonth(String strDate, int months) {
        Date date;
        try {
            date = dateFormatter.parse(strDate);
        } catch (ParseException e) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, months);
        return dateFormatterByPattern(cal.getTime(), "yyyy-MM-dd");
    }

    public static Date parse(String strDate) throws ParseException {
        return parse(strDate, getDatePattern());
    }

    public static Date parse(String strDate, String pattern) {
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        try {
            return df.parse(strDate);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public static Date parse(Date strDate, String pattern) {
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        try {
            return df.parse(df.format(strDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String dateFormatterByPattern(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        if (date != null) {
            return sdf.format(date);
        } else {
            return sdf.format(new Date());
        }
    }

    public static String getDatePattern() {
        return defaultDatePattern;
    }

    public static String format(Date date) {
        return format(date, getDatePattern());
    }

    public static String format(Date date, String pattern) {
        String returnValue = "";

        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            returnValue = df.format(date);
        }

        return (returnValue);
    }
}

Related

  1. addMonth(Date date, int month)
  2. addMonth(Date date, int month)
  3. addMonths(Date date, int iMonthCount)
  4. addMonths(Date date, int months)
  5. addMonths(int updateInterval, String dateFormat)
  6. addMonths(java.util.Date today, int monthsToAdd)