How to disable certain dates in the Android Date Picker dialog?

后端 未结 2 1649
情深已故
情深已故 2020-12-03 22:58

I am using default Android DatePickerDialog which contains android.widget.DatePicker.

Does anybody know how to disable or make certain date

2条回答
  •  一生所求
    2020-12-03 23:32

    I got a method by using custom datepicker. First we need to take the date as a string and then convert it into date object and then the date is converted to calendar object. At last by using setDisabledDays(calenderobj). We can disable it. The code is as follows.

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

提交回复
热议问题