How to disable dates before today date in DatePickerDialog Android?

前端 未结 5 999
深忆病人
深忆病人 2020-11-30 07:18

I want to disable the dates before today date in the DatePickerDialog .I am new in android please suggest me how could i do this .Here is my code that i have written for Dat

5条回答
  •  忘掉有多难
    2020-11-30 07:40

    Get the DatePicker used in your DatePickerDialog with the getDatePicker() method and use the setMinDate(Long millis) method.

    Pass to it the minimum date (in milliseconds from Epoch) you have to set.

    So you can do something like

    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    Date d = sdf.parse("21/12/2012");
    DatePicker datePicker = date.getDatePicker();
    datePicker.setMinDate(d.getTime());
    

    EDIT:

    ok, so when your creating your DatePickerDialog, before showing it, just save it to a variable, set the minimum date and then show it.

    DatePickerDialog dpd = new DatePickerDialog(IweenFlightSearch.this, date, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH));
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    Date d = sdf.parse("21/12/2012");
    dpd.getDatePicker().setMinDate(d.getTime());
    dpd.show();
    

    this should work.

提交回复
热议问题