datepickerdialog set min date code example

Example 1: set min date in datepickerdialog android

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Calendar calendar = Calendar.getInstance();
    int year    = calendar.get(Calendar.YEAR);
    int month   = calendar.get(Calendar.MONTH);
    int day     = calendar.get(Calendar.DAY_OF_MONTH);

    DatePickerDialog dialog = new DatePickerDialog(getContext(), listener, year, month, day);
    Field mDatePickerField;
    try {
            mDatePickerField = dialog.getClass().getDeclaredField("mDatePicker");
            mDatePickerField.setAccessible(true);
    } catch (Exception e) {
            e.printStackTrace();
    }
    dialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
    return dialog;
}

Example 2: Android DatePickerDialog: Set min and max date for selection

mdate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final Calendar calendar = Calendar.getInstance();
            //set time zone
            calendar.setTimeZone(TimeZone.getTimeZone("UTC"));

            int selectedYear = calendar.get(Calendar.YEAR);
            int selectedMonth = calendar.get(Calendar.MONTH);
            int selectedDay = calendar.get(Calendar.DAY_OF_MONTH);
            DatePickerDialog datePickerDialog = new DatePickerDialog(BrandResturant_DetailActivity.this,
                    new DatePickerDialog.OnDateSetListener() {
                        public void onDateSet(DatePicker view, int selectedYear,
                                              int selectedMonth, int selectedDay) {
                            mDay = selectedDay;
                            mMonth = selectedMonth;
                            mYear = selectedYear;
                            StringBuilder Date = new StringBuilder("");
                            String conver = Integer.toString(selectedYear);
                            Date.append(conver);
                            Date.append("-");
                            selectedMonth++;
                            conver = Integer.toString(selectedMonth);
                            Date.append(conver);
                            Date.append("-");
                            conver = Integer.toString(selectedDay);
                            Date.append(conver);
                            isDob = true;
                        }
                    }, mDay, mMonth, mYear);

            datePickerDialog.getDatePicker().setMaxDate(calendar.getTimeInMillis());


            
            //Set Today date to calendar
            final Calendar calendar2 = Calendar.getInstance();
            //Set Minimum date of calendar
            calendar2.set(2021, 1, 3);
            datePickerDialog.getDatePicker().setMinDate(calendar2.getTimeInMillis());
            datePickerDialog.setTitle("Select Date");
            datePickerDialog.show();


            final Calendar calendar3 = Calendar.getInstance();
            //Set Maximum date of calendar
            calendar3.set(2021, 3, 3);
            //Set One Month date from today date to calendar
            //calendar3.add(Calendar.MONTH, 1);
            datePickerDialog.getDatePicker().setMaxDate(calendar3.getTimeInMillis());
            datePickerDialog.setTitle("Select Date");
            datePickerDialog.show();
        }
    });

Tags:

Misc Example