Java Month of Year getNextMonth(Date nowdate, int delay)

Here you can find the source of getNextMonth(Date nowdate, int delay)

Description

get Next Month

License

Open Source License

Declaration

public static Date getNextMonth(Date nowdate, int delay) 

Method Source Code


//package com.java2s;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main {
    public static Date getNextMonth(Date nowdate, int delay) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Calendar temp = Calendar.getInstance();
        temp.setTime(nowdate);/* w w w.j  a  v  a 2  s.co  m*/
        int oldMonth = temp.get(Calendar.MONTH);
        int newMonth = oldMonth + delay;
        int oldYear = temp.get(Calendar.YEAR);
        int i, j;
        if (newMonth > 12) {
            i = newMonth % 12;
            j = newMonth / 12;
            temp.set(Calendar.MONTH, i);
            temp.set(Calendar.YEAR, j + oldYear);
        } else {
            temp.set(Calendar.MONTH, newMonth);
        }

        temp.set(Calendar.DATE, temp.get(Calendar.DATE));
        String dateString = formatter.format(temp.getTime());
        Date strtodate = null;
        try {
            strtodate = formatter.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return strtodate;
    }
}

Related

  1. getMonthOfYearFromTimestamp(long ms)
  2. getMonthStart(int year, int month)
  3. getNextiMonth(Date date, int month)
  4. getNextMonth()
  5. getNextMonth(Calendar aCal)
  6. getNextMonthFirstDay(String date)
  7. getNextMonthFirstDay(T day)
  8. getNextMonthFistDay(String nowdate, String inFormat, String outFormat)
  9. getNextYearMonth(String yearMonth)