如何将 android 中的日期选择器对话框设置为昨天的日期

how to set datepicker dialog in android to yesterday date

我有一个日期选择器代码,它将当前日期设置为日期选择器对话框

我如何将此日期设置为昨天的日期,特别是在月末期间它会保留昨天的日期和上个月,例如,如果在 2 月 1 日,我们检查它应该显示 1 月 31 日,从 2 月 2 日开始应该显示 1 日二月等等。

 final Calendar c = Calendar.getInstance();
        maxyear = c.get(Calendar.YEAR);
        maxmonth = c.get(Calendar.MONTH);
        maxday = c.get(Calendar.DAY_OF_MONTH);


protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
            // set date picker as current date
            DatePickerDialog _date =   new DatePickerDialog(this, datePickerListener, maxyear,maxmonth,
                    maxday){
                @Override
                public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)
                {   
                    if (year < 2014)
                        view.updateDate(2014, mmonth, mday);

                    if (monthOfYear < 12 && year == 2014)
                        view.updateDate(2014, 12, mday);

                    if (dayOfMonth < 31 && year == 2014 && monthOfYear == 12)
                        view.updateDate(2014, 12, 31);

                    if (year > maxyear)
                        view.updateDate(maxyear, maxmonth, maxday);

                    if (monthOfYear > maxmonth && year == maxyear)
                        view.updateDate(maxyear, maxmonth, maxday);

                    if (dayOfMonth > maxday && year == maxyear && monthOfYear == maxmonth)
                        view.updateDate(maxyear, maxmonth, maxday);

                }
            };
            return _date;
        }
        return null;
    }

你试过吗?

Calendar now = Calendar.getInstance();   

now.add(Calendar.DATE, -1); 

int year = now.get(Calendar.YEAR);
int month = now.get(Calendar.MONTH); // it's zero based
int day = now.get(Calendar.DAY_OF_MONTH);

DatePicker datePicker.updateDate(year, month, day);

使用更新更新日期

mDatePicker.setTitle("Please select date");
mDatePicker.updateDate(1994, 6, 12);

// TODO Hide Future Date Here
                 mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());