How to set minimum DatePicker date to current date

前端 未结 9 1737
有刺的猬
有刺的猬 2020-11-28 07:30

I want to set the minimum date the user can choose in a DatePicker to the current date. I\'ve tried this:

DatePicker datePicker = (DatePicker) findViewById(R         


        
9条回答
  •  自闭症患者
    2020-11-28 07:44

    The error says you cannot set the minimum date to exactly now. Try subtracting a second:

    datePicker.setMinDate(System.currentTimeMillis() - 1000);
    

    From the source code the minimum date must be before, not equal to, the current date:

    if (date.before(mMinDate)) {
        throw new IllegalArgumentException("fromDate: " + mMinDate.getTime()
                + " does not precede toDate: " + date.getTime());
    }
    

    So you simply need to subtract enough time from now (System.currentTimeMillis()) pass date.before(mMinDate).

提交回复
热议问题