如果您的应用程序需要在API LEVEL 8+上运行,请使用以下方式来执行此操作。将相关代码放在 youtonDateSet
方法中。
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
String selected_date = dayOf + "-" + monthOf + "-" + year;
Log.i(TAG, "Selected Date" + selected_date);
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Date strDate = null;
try {
strDate = sdf.parse(selected_date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Calendar c = Calendar.getInstance();
System.out.println("Current time => " + c.getTime());
Date current_date = null;
String formattedDate = sdf.format(c.getTime());
try {
current_date = sdf.parse(formattedDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (current_date.after(strDate)) {
Toast.makeText(getApplicationContext(),
"Please choose future date", 1).show();
} else {
mYear = String.valueOf(year);
mMonth = String.valueOf(monthOfYear);
mDay = String.valueOf(dayOfMonth);
updateStartDate();
}
}
如果您的应用程序需要在API LEVEL 11+上运行,那么使用您可以在此处DatePicker.setMinDate(long)
找到更多信息