0

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

Varun Vishnoi
  • 328
  • 6
  • 23

4 Answers4

2

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
}
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

You can use: getDatePicker().setMaxDate(). Override onCreate() of Dialog and apply limits there.

S.D.
  • 29,290
  • 3
  • 79
  • 130
1

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.

Community
  • 1
  • 1
unlimit
  • 3,672
  • 2
  • 26
  • 34
0

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

Bhavesh Patadiya
  • 25,740
  • 15
  • 81
  • 107
  • not working bcz Selected Date17-5-2013 and Current time => Mon Jun 17 13:21:54 IST 2013... These cant be compared. – Varun Vishnoi Jun 17 '13 at 07:54
  • @VarunVishnoi : seems you do not have tried my code.i have convert the current date in to same format as selected date before comparing it. you should look the code again and trying to run once in your project. – Bhavesh Patadiya Jun 17 '13 at 07:58