user7108398 user7108398 - 1 year ago 266
Android Question

Set MinDate and MaxDate in DatePIcker

Creating app in which i am showing DatePicker.Now i want to set MinDate of DatePicker is previous two years and max date future two years only.Selection should be base on current date.Suppose current Date is 23/11/2016 so datepicker should show date till 23/11/2014 in DatePicker all the date should be disabled before the 23/11/2014.And when we click on Datepicker cursor should be on current date.Created DtaePicker

private void showDateDailog() {

final DatePickerDialog datePickerDialog = new DatePickerDialog(mContext, new DatePickerDialog.OnDateSetListener() {

public void onDateSet(DatePicker datePicker, int selectedYear, int selectedMonth, int selectedDate) {

year = selectedYear;
month = selectedMonth;
day = selectedDate;

((TextView) findViewById( StringBuilder().append(day).append("/")
.append(month + 1).append("/").append(year));

}, year, month, day);

Answer Source

To set the min date two years before and max two years after today use the following code:

Calendar c = Calendar.getInstance();
c.add(Calendar.YEAR, -2); // subtract 2 years from now
c.add(Calendar.YEAR, 4); // add 4 years to min date to have 2 years after now
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download