How to disable specific dates in calendar

2019-09-22 07:26发布

问题:

How can I disable specific dates? I don't mean previous or future dates, but rather specified dates such as holidays.

回答1:

How to disable specific dates in calendar

AFAIK No You can't disable specific dates in calendar because that is That is not a built in behavior

for that purpose you have to use third party library or your have to create your own

you would have to use a custom date picker.



回答2:

At last found it.. how to disable specified dates in calendar..

    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
                    String a = "26-07-2017";
                    java.util.Date date = null;
                    try {
                        date = sdf.parse(a);
                        MainActivity obj = new MainActivity();
                        calendar = obj.dateToCalendar(date);
                        System.out.println(calendar.getTime());
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }

                    List<Calendar> dates = new ArrayList<>();
                    dates.add(calendar);
                    Calendar[] disabledDays1 = dates.toArray(new Calendar[dates.size()]);
                    dpd.setDisabledDays(disabledDays1);

private Calendar dateToCalendar(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }


回答3:

For enabling specified dates in a calendar, you can use this library.

Here if you will see the DatePickerFragment in the sample. We are passing an array of calendar dates like this.

               Calendar[] days = new Calendar[13];
                for (int i = -6; i < 7; i++) {
                    Calendar day = Calendar.getInstance();
                    day.add(Calendar.DAY_OF_MONTH, i * 2);
                    days[i + 6] = day;
                }
               dpd.setSelectableDays(days);

We can create the array of calendar dates in specified format and pass that calendar dates array to the date picker dialog to enable it.

You can achieve your goal in similar way.