如何在Android日期选择器中禁用未来日期

时间:2014-01-07 11:54:15

标签: android datepicker android-datepicker

如何在Android日期选择器中停用未来日期

Java代码:

mExpireDate.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                // To show current date in the datepicker
                final Calendar mcurrentDate = Calendar.getInstance();
                int mYear = mcurrentDate.get(Calendar.YEAR);
                int mMonth = mcurrentDate.get(Calendar.MONTH);
                int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);

                 DatePickerDialog mDatePicker = new DatePickerDialog(
                        EventRegisterActivity.this, new OnDateSetListener() {
                            public void onDateSet(DatePicker datepicker,
                                    int selectedyear, int selectedmonth,
                                    int selectedday) {

                                mcurrentDate.set(Calendar.YEAR, selectedyear);
                                mcurrentDate.set(Calendar.MONTH, selectedmonth);
                                mcurrentDate.set(Calendar.DAY_OF_MONTH,
                                        selectedday);
                                SimpleDateFormat sdf = new SimpleDateFormat(
                                        getResources().getString(
                                                R.string.date_card_formate),
                                        Locale.US);

                                mExpireDate.setText(sdf.format(mcurrentDate
                                        .getTime()));
                            }
                        }, mYear, mMonth, mDay);

                mDatePicker.setTitle(getResources().getString(
                        R.string.alert_date_select));
                mDatePicker.show();
            }
        });

怎么做?

10 个答案:

答案 0 :(得分:138)

使用DatePickerDatePickerDialog获取getDatePicker()。使用setMaxDate()

将最大日期设置为当前日期
mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());

需要API等级11。

答案 1 :(得分:15)

您可以在DatePickerDialog上调用getDatePicker()。setMaxDate(long),将今天设置为最大日期。您可以使用您发布的代码段中的相同名称更新该功能。

注意:: DatePickerDialog是我在Android文档中从我发布的链接中引用的对象。

@Override
protected Dialog onCreateDialog(int id) {
    Calendar c = Calendar.getInstance();
    int cyear = c.get(Calendar.YEAR);
    int cmonth = c.get(Calendar.MONTH);
    int cday = c.get(Calendar.DAY_OF_MONTH);
    switch (id) {
        case DATE_DIALOG_ID:
        //start changes...
        DatePickerDialog dialog = new DatePickerDialog(this, mDateSetListener, cyear, cmonth, cday);
        dialog.getDatePicker().setMaxDate(System.currentTimeMillis());
        return dialog;
        //end changes...
    }
    return null;
}

试试这个并提供反馈!!!

答案 2 :(得分:4)

如果用户选择将来的日期,则将datepicker更新为当前日期(今天)

您可以使用以下代码检查所选的是未来日期

final Calendar cal = Calendar.getInstance();
datePickerDob.init(currentYear, currentMonth, currentDay,
            new OnDateChangedListener() {

                @Override
                public void onDateChanged(DatePicker view, int year,
                        int monthOfYear, int dayOfMonth) {
                    Calendar selectedCal = Calendar.getInstance();
                    selectedCal.set(year, monthOfYear, dayOfMonth);

                    long selectedMilli = selectedCal.getTimeInMillis();

                    Date datePickerDate = new Date(selectedMilli);
                    if (datePickerDate.after(new Date())) {

                        datePickerDob.updateDate(cal.get(Calendar.YEAR),
                                cal.get(Calendar.MONTH),
                                cal.get(Calendar.DAY_OF_MONTH));

                    } else {


                    }

                }
            });

您也可以使用compareTo()方法

datePickerDate.compareTo(new Date());

将接收器与指定的日期进行比较,以确定相对排序。

参数 日期与要比较的日期。

返回 一个int<如果此Date小于指定的Date,则为0;如果它们相等则为0,并且int>如果此日期更大,则为0。

答案 3 :(得分:1)

**new Way**

 ed_date.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Calendar mcurrentDate=Calendar.getInstance();
                year=mcurrentDate.get(Calendar.YEAR);
                month=mcurrentDate.get(Calendar.MONTH);
                day=mcurrentDate.get(Calendar.DAY_OF_MONTH);

                final DatePickerDialog   mDatePicker =new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener()
                {
                    @Override
                    public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday)
                    {
                              ed_date.setText(new StringBuilder().append(year).append("-").append(month+1).append("-").append(day));
                            int month_k=selectedmonth+1;

                    }
                },year, month, day);
                mDatePicker.setTitle("Please select date");
                // TODO Hide Future Date Here
                mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());

                // TODO Hide Past Date Here
                //  mDatePicker.getDatePicker().setMinDate(System.currentTimeMillis());
                mDatePicker.show();
            }
        }); 

答案 4 :(得分:1)

如果您使用材质设计Datepicker:

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    DatePickerDialog datePickerDialog = DatePickerDialog.newInstance(this,
            calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));
    datePickerDialog.show(getFragmentManager(), "datepicker");
    datePickerDialog.setMaxDate(calendar);

答案 5 :(得分:1)

 // Custom Your Future Dates  

 // Example for today and next 3 days :- 3(NUMBER OF NEXT DAYS)*24*60*60*1000l

 // As 24 represents hrs in day

 // As 60 mins 60 secs and convert it to millisec

 //Inside Class which implements DatePickerDialog.OnDateSetListener
 private Calendar mCurrentDate;

 //Inside OnCreate Method
 mDateEditText.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {

   mCurrentDate = Calendar.getInstance();
   int year = mCurrentDate.get(Calendar.YEAR);
   int month = mCurrentDate.get(Calendar.MONTH);
   int day = mCurrentDate.get(Calendar.DAY_OF_MONTH);

   DatePickerDialog mDatePickerDialog = new DatePickerDialog(this, this, year, month, day);
   mDatePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis() + 3 * 24 * 60 * 60 * 1000 l);
  }
 });


 @Override
 public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
  mDateEditText.setText(dayOfMonth + "/" + month + "/" + year);

 }

答案 6 :(得分:0)

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    DatePickerDialog dialog = new DatePickerDialog(getActivity(), ondateSet, year, month, day);
    Calendar c = Calendar.getInstance();
    c.add(Calendar.DATE, 1);
    Date newDate = c.getTime();
    dialog.getDatePicker().setMaxDate(newDate.getTime() - (newDate.getTime() % (24 * 60 * 60 * 1000)));
    return  dialog;
}

答案 7 :(得分:0)

以下代码可帮助您禁用将来的日期:

全局声明日历变量:

private Calendar myCalendar = Calendar.getInstance();

在onCreate方法中放置以下代码:

DatePickerDialog.OnDateSetListener dateListener = new DatePickerDialog.OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
                          int dayOfMonth) {
        // TODO Auto-generated method stub
        myCalendar.set(Calendar.YEAR, year);
        myCalendar.set(Calendar.MONTH, monthOfYear);
        myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        updateLabel();
    }

};

在按钮上单击,输入以下代码:

DatePickerDialog datePickerDialog=new DatePickerDialog(getActivity(), dateListener, myCalendar
                    .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                    myCalendar.get(Calendar.DAY_OF_MONTH));

               //following line to restrict future date selection     
            datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis());
            datePickerDialog.show();

答案 8 :(得分:0)

设置直到明天的最大日期 代码中的最后一位数字代表您要设置的天数。

  pickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis() + (1000 * 60 * 60 * 24 * 1));

答案 9 :(得分:0)

在科特林

cvCalendar.maxDate = System.currentTimeMillis()