How to set a custom minimum and maximum date selection day in "wdullaer materialdatetimepicker"

2.5k Views Asked by At

I am working on a project where I need to restrict the user from choosing any other dates than I want.

by reading some articles and SO post i tried the following which is supposed to work

    com.wdullaer.materialdatetimepicker.date.DatePickerDialog dpd = newInstance(
                ActivityClaimOffers.this,
                now.get(Calendar.YEAR),
                now.get(Calendar.MONTH),
                now.get(Calendar.DAY_OF_MONTH)
        );
    dpd.setMinDate(calendar);
    dpd.setMaxDate(calendar);

but I could not figure out how to pass custom dates as calendar objects. as both setMinDate() and setMaxDate() takes Calendar as parameter

2

There are 2 best solutions below

0
On BEST ANSWER

Like this :

Calendar calendar = new GregorianCalendar(year, monthOfYear, dayOfMonth);

and for your code:

dpd.setMinDate(new GregorianCalendar(2000, 5, 23));
0
On

Hey try like this code I have done like this

Calendar nowMinimum = Calendar.getInstance();
DatePickerDialog dpd = DatePickerDialog.newInstance(
            this,
            nowMinimum.get(Calendar.YEAR),
            nowMinimum.get(Calendar.MONTH),
            nowMinimum.get(Calendar.DAY_OF_MONTH));

    dpd.setMinDate(nowMinimum); //today's date is minimum 

    Calendar thenMaximum = Calendar.getInstance();
    thenMaximum.set(year, monthOfYear, dayOfMonth);// you can pass your custom date 

    dpd.setMinDate(thenMaximum);
    dpd.setTitle("Select Date");
    dpd.show(getActivity().getFragmentManager(), "date_picker");