Ravi
Ravi

Reputation: 291

Date Picker Should not accept past dates which are less then the current date in android

I am developing one app like reminder, in this user can create the task according to date. Here I want to restrict the user to not to select the past date which are less then the system current date. So that user wont create the task for the past dates. So, can any one help me out from this.

Thanks in advance Ravi

Upvotes: 0

Views: 1612

Answers (2)

Last Warrior
Last Warrior

Reputation: 1307

i'm thinking you are using datepicker dialog.if thats right then in

OnDateSetListener dateSetListener = new OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year,
                    int monthOfYear, int dayOfMonth) {
                try {
                    Date selectedDate = new SimpleDateFormat("yyyy/MM/dd").parse(year+"/"+monthOfYear+"/"+dayOfMonth);
Date currDate = new Date();
if(selectedDate.compareTo(currDate ) >=0 ){
//then do your work
}else{
//show message
}

}catch(Exception e){
  e.getMessage();

}

Upvotes: 1

Sandeep Kumar P K
Sandeep Kumar P K

Reputation: 7482

You can achieve this using OnDateSetListener like folowing

private DatePickerDialog.OnDateSetListener mDateSetListener =
    new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, 
        int monthOfYear, int dayOfMonth) {
    //here we get the selected date, so we can compare the date with current date 
            //and perform required operation

}

Upvotes: 0

Related Questions