简体   繁体   中英

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

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();

}

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM