0

I want to add a number of months to a specific date which i got when user selects from a date picker. for example if user selects jan 31, 2016 and if number of months is 1 then it should show March 2, 2016 or March 30, 2016 should show April 30, 2016 etc. and if number of months is grater than 12, year should be added. This is my datepicker:

 @Override
    protected Dialog onCreateDialog(int id) {
        // TODO Auto-generated method stub
        if (id == 999) {
            DatePickerDialog dialog = new DatePickerDialog(this, myDateListener, year, month, day);
            dialog.getDatePicker().setMaxDate(new Date().getTime());
            return dialog;
        }
        return null;
    }

    private DatePickerDialog.OnDateSetListener myDateListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
            // TODO Auto-generated method stub
            // arg1 = year
            // arg2 = month
            // arg3 = day
            showDate(arg1, arg2+1, arg3);
        }
    };

    private void showDate(int year, int month, int day) {

        date.setText(new StringBuilder().append(day).append("/")
                .append(month).append("/").append(year));
    }

i am getting year, month, day as integer value.

Please help...i have no idea how to start?

Elizabeth
  • 1,399
  • 1
  • 13
  • 25

2 Answers2

1
public static Date addDays(Date date, int days)
{
  Calendar cal = Calendar.getInstance();
  cal.setTime(date);
  cal.add(Calendar.DATE, days); //minus number would decrement the days
  return cal.getTime();
}

copied from https://stackoverflow.com/a/18143654/1811771

N.B.if you want to make Date from day, month, year

String str = year + "/" + month + "/" + day;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
Date date = formatter.parse(str);
Community
  • 1
  • 1
shourav
  • 946
  • 9
  • 15
1

Try this one. This works for me.

// if you want date after 60 days. yourCustomDate - is date from which you want to set new date

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(yourCustomDate);
        calendar.add(Calendar.DAY_OF_YEAR, 60);
        Date afterSixtyDaysDate = calendar.getTime();
Bhoomika
  • 105
  • 1
  • 7