How to disable dates after today date in DatePickerDialog Android?

pRaNaY picture pRaNaY · Jun 16, 2016 · Viewed 7.9k times · Source

I want to disable all Next(after) dates from today date in DatePickerDialog.

I am using below code to create DatePickerDialog:

    Calendar calendar = Calendar.getInstance();
    mYear = calendar.get(Calendar.YEAR);
    mMonth = calendar.get(Calendar.MONTH);
    mDay = calendar.get(Calendar.DAY_OF_MONTH);

 //OnDateSetListener
 private DatePickerDialog.OnDateSetListener myDateListener = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker arg0, int year, int monthOfYear, int dayOfMonth) {
        calendar.set(year, monthOfYear, dayOfMonth);
        String currentDate = date_format.format(calendar.getTime());
        ...
    }
};
 //Create DatePickerDialog here
 protected Dialog onCreateDialog(int id) {
    if (id == 999) {
        DatePickerDialog dpDialog = new DatePickerDialog(getContext(), myDateListener, mYear, mMonth, mDay);

        return dpDialog;
    }
    return null;
}

Please suggest what I have to do for disable all previous date in datepicker dialog.

Answer

Ravi picture Ravi · Jun 16, 2016

Just pass current date in setMaxDate() of DatePickerDialog.

DatePickerDialog dpDialog = new DatePickerDialog(getContext(), myDateListener, mYear, mMonth, mDay);
dpDialog.getDatePicker().setMaxDate(calendar.getTimeInMillis());

or

dpDialog.getDatePicker().setMaxDate(System.currentTimeMillis());