59

我想将用户可以在 DatePicker 中选择的最小日期设置为当前日期。我试过这个:

DatePicker datePicker = (DatePicker) findViewById(R.id.event_date);
datePicker.setMinDate(System.currentTimeMillis());

这给了我以下例外:

12-01 12:23:31.226: E/AndroidRuntime(10311): Caused by: java.lang.IllegalArgumentException: fromDate: Sat Dec 01 12:23:31 EST 2012 does not precede toDate: Sat Dec 01 12:23:31 EST 2012

我该怎么做呢?

4

10 回答 10

171

该错误表示您无法将最短日期设置为现在。尝试减去一秒:

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

从源代码来看,最小日期必须早于,不等于,当前日期:

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

所以你只需要从 now ( System.currentTimeMillis()) pass中减去足够的时间date.before(mMinDate)

于 2012-12-01T17:41:52.977 回答
16

如果您不想使用自定义对话框。使用这个单行代码:

datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis()); 
于 2016-07-02T10:01:06.850 回答
8

检查 Android DatePickerDialog设置最小最大日期代码。

这里是例子

final 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 dpd = new DatePickerDialog(getActivity(),AlertDialog.THEME_TRADITIONAL,this,year, month, day);  

    //Get the DatePicker instance from DatePickerDialog  
    DatePicker dp = dpd.getDatePicker();  
    //Set the DatePicker minimum date selection to current date  
    dp.setMinDate(c.getTimeInMillis());//get the current day  
    //dp.setMinDate(System.currentTimeMillis() - 1000);// Alternate way to get the current day  

    //Add 6 days with current date  
    c.add(Calendar.DAY_OF_MONTH,6);  

    //Set the maximum date to select from DatePickerDialog  
    dp.setMaxDate(c.getTimeInMillis());  
    //Now DatePickerDialog have 7 days range to get selection any one from those dates 
于 2015-05-09T07:28:57.550 回答
8

这对我来说非常有效。简单而美好。

DatePickerDialog dialog = new DatePickerDialog(this, this,
              Calendar.YEAR,Calendar.MONTH,
                Calendar.DAY_OF_MONTH);

        dialog.getDatePicker().setMinDate(Calendar.getInstance().getTimeInMillis());
        dialog.show();
于 2018-06-27T19:31:21.180 回答
4

将此行添加到您的 DatePicker Fragment 会将最小日期设置为当前日期并禁用前几个月。

datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis());
于 2017-04-19T11:56:59.187 回答
0

这可以帮助你。

Calendar cal=Calendar.getInstance();

int year=cal.get(Calendar.YEAR);
int month=cal.get(Calendar.MONTH);
int day=cal.get(Calendar.DAY_OF_MONTH);
int hour=cal.get(Calendar.HOUR_OF_DAY);
int min=cal.get(Calendar.MINUTE);

datePicker.updateDate(year, month, day);

timePicker.setCurrentHour(hour);
timePicker.setCurrentMinute(min);
于 2012-12-01T17:31:13.813 回答
0

试试这个

datePicker.setMinDate(new GregorianCalendar.getTimeInMillis());
于 2012-12-01T17:41:11.300 回答
0
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis()); 

或者

DatePickerDialog dialog = new DatePickerDialog(this, this,
          Calendar.YEAR,Calendar.MONTH,
            Calendar.DAY_OF_MONTH);

    dialog.getDatePicker().setMinDate(Calendar.getInstance().getTimeInMillis());
    dialog.show();
于 2019-06-12T10:26:18.963 回答
0
datePicker.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);

最短的日期不能是瞬间

于 2020-04-23T12:41:23.950 回答
0

对于自定义日期

Calendar c = Calendar.getInstance();
c.set(2017, 0, 1);//Year,Month -1,Day
your_date_picker.setMaxDate(c.getTimeInMillis());
于 2021-10-13T18:02:49.350 回答