Java Month Convert nextMonth(Date date)

Here you can find the source of nextMonth(Date date)

Description

next Month

License

Open Source License

Declaration

public static Date nextMonth(Date date) 

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 nextMonth(Date date) {
        Calendar temp = Calendar.getInstance();
        temp.setTime(date);//  w  w  w.  j  a  v  a2 s  .c  o  m

        int y = temp.get(Calendar.YEAR);
        int m = temp.get(Calendar.MONTH) + 1;
        int d = temp.get(Calendar.DAY_OF_MONTH);

        String sj1 = "";

        if (d >= 1 && d <= 20) {
            d = 21;
            sj1 = String.valueOf(y) + "-" + String.valueOf(m) + "-" + String.valueOf(d);
        } else if (d >= 21) {
            if (m == 12) {
                y += 1;
                m = 1;
            } else {
                m += 1;
            }
            d = 21;

            sj1 = String.valueOf(y) + "-" + String.valueOf(m) + "-" + String.valueOf(d);
        }

        SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
        Date r_date = null;
        try {
            r_date = myFormatter.parse(sj1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return r_date;
    }
}

Related

  1. monthOfDate(Date s)
  2. monthsBetween(String from, String to)
  3. monthStringToInt(String month)
  4. nextMonth()
  5. nextMonth(Date date)
  6. parseMonth(final String source)
  7. setMonths(Date date, int amount)