Disable whole week except weekend in calender Andr

2019-06-11 21:05发布

问题:

Im currently Using MaterialDateTimePicker library for date and time picker, But im facing issue where i cant disable whole week except certain days that i get from API .

In the library it says you can achieve something like this by using

  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);

but how do i change that to disable week days by name

回答1:

After some searching and adjusting i managed to do it with the following code :

                Calendar day;
                List<Calendar> weekends = new ArrayList<>();
                int weeks = 5;

                for (int i = 0; i < (weeks * 7); i = i + 7) {

                    for (int d = 0; d < slots.getResponse().getDays().size(); d++) {

                        day = Calendar.getInstance();
                        switch (slots.getResponse().getDays().get(d)) {
                            case "Sunday":
                                day.add(Calendar.DAY_OF_YEAR, (Calendar.SUNDAY - day.get(Calendar.DAY_OF_WEEK) + i));
                                weekends.add(day);
                                break;
                            case "Monday":
                                day.add(Calendar.DAY_OF_YEAR, (Calendar.MONDAY - day.get(Calendar.DAY_OF_WEEK) + i));
                                weekends.add(day);
                                break;
                            case "Tuesday":
                                day.add(Calendar.DAY_OF_YEAR, (Calendar.TUESDAY - day.get(Calendar.DAY_OF_WEEK) + i));
                                weekends.add(day);
                                break;
                            case "Wednesday":
                                day.add(Calendar.DAY_OF_YEAR, (Calendar.WEDNESDAY - day.get(Calendar.DAY_OF_WEEK) + i));
                                weekends.add(day);
                                break;
                            case "Thursday":
                                day.add(Calendar.DAY_OF_YEAR, (Calendar.THURSDAY - day.get(Calendar.DAY_OF_WEEK) + i));
                                weekends.add(day);
                                break;
                            case "Friday":
                                day.add(Calendar.DAY_OF_YEAR, (Calendar.FRIDAY - day.get(Calendar.DAY_OF_WEEK) + i));
                                weekends.add(day);
                                break;
                            case "Saturday":
                                day.add(Calendar.DAY_OF_YEAR, (Calendar.SATURDAY - day.get(Calendar.DAY_OF_WEEK) + i));
                                weekends.add(day);
                                break;

                        }

                    }
                }
                Calendar[] SelectableDays = weekends.toArray(new Calendar[weekends.size()]);
                dpd.setSelectableDays(SelectableDays);