3

I would like to do when user select the past date in datepicker, it should not accept yesterday date. My application will prompt message to inform user cant select less than current date.anyone guide me please ?...Thank you

mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
            Button pickDate_btn = (Button) findViewById(R.id.pickDate);
            pickDate_btn.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                showDialog(DATE_DIALOG_ID);
            }
        });

        final Calendar now = Calendar.getInstance();
        mYear = now.get(Calendar.YEAR);
        mMonth = now.get(Calendar.MONTH);
        mDay = now.get(Calendar.DAY_OF_MONTH);

        updateDisplay();      

@Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DATE_DIALOG_ID:
                return new DatePickerDialog(this,
                            mDateSetListener,
                            mYear, mMonth, mDay);
        }
        return null;
    }

    @Override
    protected void onPrepareDialog(int id, Dialog dialog) {
        switch (id) {

            case DATE_DIALOG_ID:
                ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
                break;
        }
    }    

    private void updateDisplay() {
        mDateDisplay.setText(
            new StringBuilder()
                    // Month is 0 based so add 1
                    .append(mDay).append("-")
                    .append(mMonth + 1).append("-")
                    .append(mYear).append(" "));
    }

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

    @Override
    public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth) {
                    // TODO Auto-generated method stub

            mYear = year;
                    mMonth = monthOfYear;
                    mDay = dayOfMonth;
                    updateDisplay();

    //should add code here?             
                }
            };
Ameen Khan
  • 19
  • 8
Jean_n
  • 45
  • 1
  • 3
  • 10

2 Answers2

7

Since API level 11 there is a method for that:

DatePicker.setMinDate(new Date().getTime())

Or if its not worked (old API) then,

Date datePickerDate = // get the value here;
Date currentDate = new Date();
if (datePickerDate.before(currentDate)) {
  // error !
}
AbdelHady
  • 9,334
  • 8
  • 56
  • 83
user370305
  • 108,599
  • 23
  • 164
  • 151
  • 1
    If the DatePicker is shown with a CalendarView, setMinDate does not prevent the user to select a date in the past. It only greys out dates 'before' the minDate. setMinDate only works when DatePicker is shown with Spinners. – Antonio Sesto May 09 '15 at 16:33
  • 1
    @AntonioSesto, I use https://github.com/wdullaer/MaterialDateTimePicker, it doesn't have this bug. – CoolMind Nov 30 '16 at 08:55
3

We can not set any attribute which limits DatePicker to select a date which is not past, but we can do it programmatically, by:

if(dateObj1.before(dateObj2) || dateObj1.equals(dateObj2)){
//the program runs normally
}
else{

                new AlertDialog.Builder(PM_Edit.this)

                .setTitle("Wrong Data Input!")

                .setMessage("The end Date must be Before the start Date, please insert new Date values")

                .setNeutralButton("Ok",

                new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog,

                int which) {

                }

                }).show();
            }
jeet
  • 29,001
  • 6
  • 52
  • 53
  • dateObj1 n dateObj2 are textview ? after select the datepicker? we only compare? – Jean_n Feb 29 '12 at 06:26
  • no these are not text views these are Date types, create current(dateObj2) by new Date(currentTimeMillis()) and dateOvject1= new Date(year - 1900, month, day); – jeet Feb 29 '12 at 06:36