Reputation: 328
I am using datepicker dialog in my app. I dont want to select passed dates when user selects any date from Dialog. My code is here.
private DatePickerDialog.OnDateSetListener mdateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int yr, int monthOfYear,
int dayOfMonth) {
year = yr;
month = monthOfYear;
day = dayOfMonth;
updateDate();
}
};
Update date sets the selected date to EditText. Is there any library method to prevent selecting past dates?
Thanks in Advance
Upvotes: 0
Views: 3729
Reputation: 25830
if your application requires to run on API LEVEL 8+ then use below way to do it. put the relevant code inside yout onDateSet
method.
@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();
}
}
if your Application require to run on API LEVEL 11+ then use DatePicker.setMinDate(long)
you can find more information HERE
Upvotes: 0
Reputation: 3752
If using API 11 or greater, you can use DatePicker.setMinDate()
Check http://developer.android.com/reference/android/widget/DatePicker.html#setMinDate(long) for more info.
Also see setMinDate() for DatePicker doesn't work to see how to use it.
Upvotes: 1
Reputation: 29436
You can use: getDatePicker().setMaxDate()
. Override onCreate()
of Dialog and apply limits there.
Upvotes: 1
Reputation: 121998
In update method your condition would be .Date class have before ,equals and after methods.Use them.
if(userdate.before(today) || userdate.equals(today)){
//past date found
}
Upvotes: 2