Disables set of dates using materialdatetimepicker

172 views Asked by At

I am using materialdatetimepicker to customise my calendar. My requirement is I want to disable all dates from today and next 31 days.

What I have tried.

1 - Set the minimum date to Today - datePickerDialog.setMinDate(now);

2 - Maximum date to 31 days from today. - datePickerDialog.setMaxDate(cal);

3 - Disabled the dates in between. datePickerDialog.setDisabledDays(otherCalendars);

When I do step 1 2 and 3 together, the Calendar is not opened at all and app freezes.

When I do the steps (1,2) and 3 seperately I get the correct results. But I only want to display next 31days from now and disable them all.

Here is the code which I have tried.

Where am I getting this wrong? any help is much appreciated.

        private DatePickerDialog datePickerDialog;

        DateTime startDateTime = new DateTime();
        DateTime endDateTime = new DateTime();
        endDateTime = endDateTime.plusDays(31);

        List<DateTime> otherDays = new ArrayList<>();
        while (startDateTime.isBefore(endDateTime)) {
            otherDays.add(startDateTime);
            startDateTime = startDateTime.plusDays(1);
        }

        Calendar[] otherCalendars = new Calendar[otherDays.size()];
                for (int count = 0; count < otherDays.size(); count++) {
                    otherCalendars[count] = otherDays.get(count).toGregorianCalendar();
                }

         datePickerDialog.setMinDate(now);
         Calendar cal = Calendar.getInstance();
         Date ddd = endDateTime.toDate();
         cal.setTime(ddd);
         datePickerDialog.setMaxDate(cal);
         datePickerDialog.setDisabledDays(otherCalendars);

Thanks R

1

There are 1 answers

0
Hossam Hassan On

I had the same issue and found solution for it here Disable whole week except weekend in calender [disable-whole-week-except-weekends]

I used List with Calendar type List<Calendar> weekends = new ArrayList<>(); And fill it with days i want to disable.