Java Month Add addMonths(String s, int month)

Here you can find the source of addMonths(String s, int month)

Description

add Months

License

Open Source License

Declaration

public static String addMonths(String s, int month) throws Exception 

Method Source Code

//package com.java2s;

public class Main {

    public static String addMonths(String s, int month) throws Exception {
        return addMonths(s, month, "yyyyMMdd");
    }/* www . j a  v  a  2s.  c  om*/

    public static String addMonths(String s, int addMonth, String format) throws Exception {
        try {

            java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(format, java.util.Locale.CHINA);
            java.util.Date date = check(s, format);

            java.text.SimpleDateFormat yearFormat = new java.text.SimpleDateFormat("yyyy", java.util.Locale.CHINA);
            java.text.SimpleDateFormat monthFormat = new java.text.SimpleDateFormat("MM", java.util.Locale.CHINA);
            java.text.SimpleDateFormat dayFormat = new java.text.SimpleDateFormat("dd", java.util.Locale.CHINA);
            int year = Integer.parseInt(yearFormat.format(date));
            int month = Integer.parseInt(monthFormat.format(date));
            int day = Integer.parseInt(dayFormat.format(date));

            month += addMonth;
            if (addMonth > 0) {
                while (month > 12) {
                    month -= 12;
                    year += 1;
                }
            } else {
                while (month <= 0) {
                    month += 12;
                    year -= 1;
                }
            }
            java.text.DecimalFormat fourDf = new java.text.DecimalFormat("0000");
            java.text.DecimalFormat twoDf = new java.text.DecimalFormat("00");
            String tempDate = String.valueOf(fourDf.format(year)) + String.valueOf(twoDf.format(month))
                    + String.valueOf(twoDf.format(day));
            java.util.Date targetDate = null;

            try {
                targetDate = check(tempDate, "yyyyMMdd");
            } catch (java.text.ParseException pe) {
                day = lastDay(year, month);
                tempDate = String.valueOf(fourDf.format(year)) + String.valueOf(twoDf.format(month))
                        + String.valueOf(twoDf.format(day));
                targetDate = check(tempDate, "yyyyMMdd");
            }

            return formatter.format(targetDate);
        } catch (Exception e) {
            throw new Exception("[DateUtil][addMonths]" + e.getMessage(), e);
        }
    }

    /**
     * check date string validation with an user defined format.
     *
     * @param s
     *            date string you want to check.
     * @param format
     *            string representation of the date format. For example,
     *            "yyyy-MM-dd".
     * @return date java.util.Date
     */
    private static java.util.Date check(String s, String format) throws java.text.ParseException {
        if (s == null)
            throw new java.text.ParseException("date string to check is null", 0);
        if (format == null)
            throw new java.text.ParseException("format string to check date is null", 0);

        java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(format, java.util.Locale.CHINA);
        java.util.Date date = null;
        try {
            date = formatter.parse(s);
        } catch (java.text.ParseException e) {
            /*
             * throw new java.text.ParseException( e.getMessage() + " with
             * format \"" + format + "\"", e.getErrorOffset() );
             */
            throw new java.text.ParseException(" wrong date:\"" + s + "\" with format \"" + format + "\"", 0);
        }

        if (!formatter.format(date).equals(s))
            throw new java.text.ParseException("Out of bound date:\"" + s + "\" with format \"" + format + "\"", 0);
        return date;
    }

    private static int lastDay(int year, int month) throws java.text.ParseException {
        int day = 0;
        switch (month) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            day = 31;
            break;
        case 2:
            if ((year % 4) == 0) {
                if ((year % 100) == 0 && (year % 400) != 0) {
                    day = 28;
                } else {
                    day = 29;
                }
            } else {
                day = 28;
            }
            break;
        default:
            day = 30;
        }
        return day;
    }
}

Related

  1. addMonth(Date date, int months)
  2. addMonths(Date date, int iMonthCount)
  3. addMonths(Date date, int months)
  4. addMonths(int updateInterval, String dateFormat)
  5. addMonths(java.util.Date today, int monthsToAdd)
  6. addMonths2(String dateStr, int months)
  7. addMonthsAndDays(Date date, int months, int days)
  8. addMonthTimetamp(long lTimeStamp, String _pattern, int month)
  9. getEndOfMonth(Date currentDate)