DatePickerDialog setMaxDate显示一个月和一天的额外

时间:2014-08-28 09:33:26

标签: java android datepicker

我有一个像这样的对话dAte选择器:

                     public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Calendar calendar = Calendar.getInstance();
        int yy = calendar.get(Calendar.YEAR);
        int mm = calendar.get(Calendar.MONTH);
        int dd = calendar.get(Calendar.DAY_OF_MONTH);
        DatePickerDialog datePicker = new DatePickerDialog(getActivity(), this, yy, mm, dd);
        datePicker.getDatePicker().setCalendarViewShown(false);

        datePicker.getDatePicker().setMaxDate(new Date().getTime());

        datePicker.setTitle("RENTRE TA DATE DE NAISSANCE.");

        return datePicker;
    }

...

我们可以看到我的问题在图像上今天的日期是2014年7月27日我希望日期选择器在此日期停止并且不显示28 aout 感谢您的帮助

1 个答案:

答案 0 :(得分:2)

将1天1个月添加到您的日历实例中。并将其传递给setMaxDate()

    // Set calendar to 1 day next from today
    calendar.add(Calendar.DAY_OF_MONTH, 1);
    // Set calendar to 1 month next
    calendar.add(Calendar.MONTH, 1);
    datePicker.getDatePicker().setMaxDate(calendar.getTimeInMillis());

所以你的方法看起来像

public Dialog onCreateDialog(Bundle savedInstanceState) {
    final Calendar calendar = Calendar.getInstance();
    int yy = calendar.get(Calendar.YEAR);
    int mm = calendar.get(Calendar.MONTH);
    int dd = calendar.get(Calendar.DAY_OF_MONTH);
    DatePickerDialog datePicker = new DatePickerDialog(getActivity(), this,
            yy, mm, dd);
    datePicker.getDatePicker().setCalendarViewShown(false);
    // Set calendar to 1 day next from today
    calendar.add(Calendar.DAY_OF_MONTH, 1);
    // Set calendar to 1 month next
    calendar.add(Calendar.MONTH, 1);
    datePicker.getDatePicker().setMaxDate(calendar.getTimeInMillis());

    datePicker.setTitle("RENTRE TA DATE DE NAISSANCE.");

    return datePicker;
}