user3069590
user3069590

Reputation: 129

How to block only past dates in DatePicker in android

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;
            }

Upvotes: 2

Views: 13877

Answers (4)

Illegal Argument
Illegal Argument

Reputation: 10338

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.

Upvotes: 1

Gaurav Srivastava
Gaurav Srivastava

Reputation: 363

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.

Upvotes: 3

MysticMagicϡ
MysticMagicϡ

Reputation: 28823

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.

Upvotes: 0

Blaze Tama
Blaze Tama

Reputation: 10948

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?

Upvotes: 1

Related Questions