How to block only past dates in DatePicker in android

12,836

Solution 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.

Solution 2

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.

Solution 3

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?

Share:
12,836
user3069590
Author by

user3069590

Updated on June 11, 2022

Comments

  • user3069590
    user3069590 almost 2 years

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