Java Month Format addMonths(String src, int addMonth, String format)

Here you can find the source of addMonths(String src, int addMonth, String format)

Description

add Months

License

Apache License

Declaration

public static String addMonths(String src, int addMonth, String format) throws IOException 

Method Source Code

//package com.java2s;
/**/*from ww w  .  jav  a  2 s . co m*/
 * Copyright 2014 tgrape Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.IOException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class Main {
    public static String addMonths(String src, int addMonth, String format) throws IOException {

        SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.UK);
        SimpleDateFormat informatter = new SimpleDateFormat("yyyyMMdd", Locale.UK);
        SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy", Locale.UK);
        SimpleDateFormat monthFormat = new SimpleDateFormat("MM", Locale.UK);
        SimpleDateFormat dayFormat = new SimpleDateFormat("dd", Locale.UK);
        DecimalFormat fourDf = new DecimalFormat("0000");
        DecimalFormat twoDf = new DecimalFormat("00");

        Calendar calendar = Calendar.getInstance(Locale.UK);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setMinimalDaysInFirstWeek(4);

        Date date = null;
        try {
            date = formatter.parse(src);
        } catch (ParseException e) {
            throw new IOException(e.getMessage());
        }

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

        String tempdate = String.valueOf(fourDf.format(year)) + String.valueOf(twoDf.format(month))
                + String.valueOf(twoDf.format(day));

        Date targetdate = null;
        try {
            targetdate = informatter.parse(tempdate);
        } catch (ParseException e) {
            throw new IOException(e.getMessage());
        }

        return formatter.format(targetdate);
    }
}

Related

  1. formatMonth(Date date)
  2. formatMonth(Date date)
  3. formatMonth(final Date date)
  4. formatMonth(final int year, final int month)