Addy Addy - 1 year ago 198
Android Question

How to disable past dates in Android date picker?

How to disable past dates in Android date picker?

protected Dialog onCreateDialog(int id) {
switch (id) {
// set date picker as current date
return new DatePickerDialog(this, datePickerListener, year, month,
return null;

private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth+1;
day = selectedDay;

startdate.setText(new StringBuilder().append(day).append("-")
.append(getMonth(month + 1)).append("-").append(year)
.append(" "));

Answer Source

You can do

datePicker.setMinDate(System.currentTimeMillis() - 1000);

which sets today's date as minimum date and all the past dates are disabled.

datePicker is an object of DatePicker if you are using an object of DatePickerDialog you can do

datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);

Note: setMinDate was introduced in API 11

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download