如何设置日期选择器的最大和最小限制?

时间:2021-04-06 16:01:04

I am using date picker in my application couples of time. I want my date picker to show dates following some restrictions. I am using the below code:-

我在我的应用程序中使用日期选择器。我希望我的日期选择器显示一些限制后的日期。我使用以下代码: -

@Override
    protected Dialog onCreateDialog(int id) {
        switch(id) {
            case DATE_PICKER_DIALOG:
                datePickerDialogDateSetListener = new DatePickerDialog.OnDateSetListener() {
                    public void onDateSet(DatePicker datePicker, int selectedYear, int selectedMonth, int selectedDate) {
                        year = selectedYear;
                        month = selectedMonth;
                        date = selectedDate;

                        activitySignUpDateEditText.setText(new StringBuilder().append(date));
                        activitySignUpMonthEditText.setText(new StringBuilder().append(month + 1));
                        activitySignUpYearEditText.setText(new StringBuilder().append(year));
                    }
                };

                return new DatePickerDialog(this, datePickerDialogDateSetListener, year, month, date);

            default:
                return null;
        }
    } 

Somewhere i want the date picker to show maximum date 18 years back from current date & somewhere minimum date as the current date. I have searched a lot but doesn't found any satisfactory answer. I know about setMaxDate() & setMinDate() methods of date picker but when i tried with it, i am unable to set selected date on my edit text. any help would be appreciable. Thanks in advance.

在某个地方,我希望日期选择器显示从当前日期起18年后的最大日期,以及当前日期的最小日期。我搜索了很多,但没有找到任何满意的答案。我知道日期选择器的setMaxDate()和setMinDate()方法但是当我尝试使用它时,我无法在编辑文本上设置选定的日期。任何帮助都会很明显。提前致谢。

2 个解决方案

#1


You can try replacing this line:

您可以尝试替换此行:

return new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay);

By those:

DatePickerDialog dpDialog = new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay);
DatePicker datePicker = dpDialog.getDatePicker();

Calendar calendar = Calendar.getInstance();//get the current day
datePicker.setMaxDate(calendar.getTimeInMillis());//set the current day as the max date
return dpDialog;

in the same way you set the minimum date as well

以同样的方式设置最小日期

DatePickerDialog dpDialog = new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay);
DatePicker datePicker = dpDialog.getDatePicker();

Calendar calendar = Calendar.getInstance();//get the current day
datePicker.setMinDate(calendar.getTimeInMillis());//set the current day as the max date
return dpDialog;

#2


For setting Max date as 18 years back, the following code snippet will do the job:

要将最大日期设置为18年后,以下代码段将完成此任务:

Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dateDialog = new DatePickerDialog(this, datePickerListener, year - 18, month, day);

//For setting minimum date as Current Date
//dateDialog.getDatePicker().setMinDate(c.getTimeInMillis ());

//For setting maximum date as 18 years back
c.set(year - 18, month, day);
dateDialog.getDatePicker().setMaxDate(c.getTimeInMillis());
return dateDialog;

As MaxDate can't be less than MinDate thus, MinDate should not be set as current date when MaxDate is set as 18 years back.

由于MaxDate不能小于MinDate,因此当MaxDate设置为18年后,不应将MinDate设置为当前日期。

Hope it'll work for you.

希望它对你有用。

#1


You can try replacing this line:

您可以尝试替换此行:

return new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay);

By those:

DatePickerDialog dpDialog = new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay);
DatePicker datePicker = dpDialog.getDatePicker();

Calendar calendar = Calendar.getInstance();//get the current day
datePicker.setMaxDate(calendar.getTimeInMillis());//set the current day as the max date
return dpDialog;

in the same way you set the minimum date as well

以同样的方式设置最小日期

DatePickerDialog dpDialog = new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay);
DatePicker datePicker = dpDialog.getDatePicker();

Calendar calendar = Calendar.getInstance();//get the current day
datePicker.setMinDate(calendar.getTimeInMillis());//set the current day as the max date
return dpDialog;

#2


For setting Max date as 18 years back, the following code snippet will do the job:

要将最大日期设置为18年后,以下代码段将完成此任务:

Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dateDialog = new DatePickerDialog(this, datePickerListener, year - 18, month, day);

//For setting minimum date as Current Date
//dateDialog.getDatePicker().setMinDate(c.getTimeInMillis ());

//For setting maximum date as 18 years back
c.set(year - 18, month, day);
dateDialog.getDatePicker().setMaxDate(c.getTimeInMillis());
return dateDialog;

As MaxDate can't be less than MinDate thus, MinDate should not be set as current date when MaxDate is set as 18 years back.

由于MaxDate不能小于MinDate,因此当MaxDate设置为18年后,不应将MinDate设置为当前日期。

Hope it'll work for you.

希望它对你有用。