我想设置,用户可以在一个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
我该怎么做呢?
错误说你不能最小日期设置为准确了。 尝试减去第二:
datePicker.setMinDate(System.currentTimeMillis() - 1000);
从源代码的最小日期必须之前,不等于,当前日期:
if (date.before(mMinDate)) {
throw new IllegalArgumentException("fromDate: " + mMinDate.getTime()
+ " does not precede toDate: " + date.getTime());
}
所以,你只需从现在减去足够的时间( System.currentTimeMillis()
通过date.before(mMinDate)
如果你不想使用自定义对话框。 使用此一行代码:
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis());
检查的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
这非常适合我。 方便,美观大方。
DatePickerDialog dialog = new DatePickerDialog(this, this,
Calendar.YEAR,Calendar.MONTH,
Calendar.DAY_OF_MONTH);
dialog.getDatePicker().setMinDate(Calendar.getInstance().getTimeInMillis());
dialog.show();
添加此行到您的DatePicker片段将设置最小日期当前日期和禁用前几个月。
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis());
这可以帮助你。
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);
试试这个
datePicker.setMinDate(new GregorianCalendar.getTimeInMillis());