2

how to block only past date in DatePicker in android? Am using sample code in that past date and current date getting blocked , I need only past date get blocked not current date here is my code

private DatePickerDialog.OnDateSetListener mDateSetListener =
        new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int yearSelected,
                                  int monthOfYear, int dayOfMonth) {
               year = yearSelected;
               month = monthOfYear;
               day = dayOfMonth;
               int n_mon = monthOfYear+1;


               business_date_et.setText(day+"-"+n_mon+"-"+year);
            }
        };


        private TimePickerDialog.OnTimeSetListener mTimeSetListener =
            new TimePickerDialog.OnTimeSetListener() {

                public void onTimeSet(TimePicker view, int hourOfDay, int min) {
                    hour = hourOfDay;
                    minute = min;

                    business_time_et.setText(hour+":"+minute);
                  }
            };


            protected Dialog onCreateDialog(int id) {
                switch (id) {
                case DATE_DIALOG_ID:

                    DatePickerDialog da = new DatePickerDialog(this, mDateSetListener,
                            mYear, mMonth, mDay);
                    Calendar c = Calendar.getInstance();

                    c.add(Calendar.DATE, 1);
                    Date newDate = c.getTime();
                    da.getDatePicker().setMinDate(newDate.getTime());
                    return da;

                case TIME_DIALOG_ID:
                    return new TimePickerDialog(this,
                            mTimeSetListener, mHour, mMinute, false);

                }
                return null;
            }
user3069590
  • 129
  • 1
  • 2
  • 11

4 Answers4

3

Here is a sample code:

DatePicker datePicker = (DatePicker) getView().findViewById(R.id.your_datepicker_id);   

final Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, c.getMinimum(Calendar.HOUR_OF_DAY));
c.set(Calendar.MINUTE, c.getMinimum(Calendar.MINUTE));
c.set(Calendar.SECOND, c.getMinimum(Calendar.SECOND));
c.set(Calendar.MILLISECOND, c.getMinimum(Calendar.MILLISECOND));

Time now = new Time();
now.setToNow();

datePicker.updateDate(now.year, now.month, now.monthDay);
datePicker.setMinDate(c.getTimeInMillis());

Please note above that you must set MinDate value to the minimum time of the current day and not just the time which is 1 or 2 seconds behind the current time. This takes care of a well know problem of Datepicker in Android.

1

Use setMinDate method (minimum API 11):

datePicker.setMinDate(System.currentTimeMillis() - 1000);

For more information regarding your topic :

How to disable dates before today date in DatePickerDialog Android?

How to disable past dates in Android date picker?

Community
  • 1
  • 1
Blaze Tama
  • 10,828
  • 13
  • 69
  • 129
1

You can also try this:

da.getDatePicker().setMinDate(newDate.getTime()-(newDate.getTime()%(24*60*60*1000)));

It basically subtracts the current date by the current offset of the day.

Illegal Argument
  • 10,090
  • 2
  • 44
  • 61
0

You can get date of 1 day after current day with this:

c.add(Calendar.DATE, 1);

So by using

c.add(Calendar.DATE, 1);

You are setting date of tomorrow as Minimum date. So just change that to

c.add(Calendar.DATE, 0);

And it should set today's date as minimum date in datepicker.

Hope it helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124