How to set minimum DatePicker date to current date

85,933

Solution 1

The error says you cannot set the minimum date to exactly now. Try subtracting a second:

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

From the source code the minimum date must be before, not equal to, the current date:

if (date.before(mMinDate)) {
    throw new IllegalArgumentException("fromDate: " + mMinDate.getTime()
            + " does not precede toDate: " + date.getTime());
}

So you simply need to subtract enough time from now (System.currentTimeMillis()) pass date.before(mMinDate).

Solution 2

If you don't want to use a custom dialog. Use this single line code:

datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis()); 

Solution 3

This worked perfectly for me. easy and nice.

DatePickerDialog dialog = new DatePickerDialog(this, this,
              Calendar.YEAR,Calendar.MONTH,
                Calendar.DAY_OF_MONTH);

        dialog.getDatePicker().setMinDate(Calendar.getInstance().getTimeInMillis());
        dialog.show();

Solution 4

Check the Android DatePickerDialog set minimum and maximum date code.

Here is the examples.

final Calendar c = Calendar.getInstance();  
    int year = c.get(Calendar.YEAR);  
    int month = c.get(Calendar.MONTH);  
    int day = c.get(Calendar.DAY_OF_MONTH);  
DatePickerDialog dpd = new DatePickerDialog(getActivity(),AlertDialog.THEME_TRADITIONAL,this,year, month, day);  

    //Get the DatePicker instance from DatePickerDialog  
    DatePicker dp = dpd.getDatePicker();  
    //Set the DatePicker minimum date selection to current date  
    dp.setMinDate(c.getTimeInMillis());//get the current day  
    //dp.setMinDate(System.currentTimeMillis() - 1000);// Alternate way to get the current day  

    //Add 6 days with current date  
    c.add(Calendar.DAY_OF_MONTH,6);  

    //Set the maximum date to select from DatePickerDialog  
    dp.setMaxDate(c.getTimeInMillis());  
    //Now DatePickerDialog have 7 days range to get selection any one from those dates 

Solution 5

Adding this line to your DatePicker Fragment will set the minimum date as current date & disable the previous months.

datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis());
Share:
85,933
gsgx
Author by

gsgx

I'm currently studying computer science at the University of Michigan. I enjoy working on operating systems and compilers, developing Android applications, and hacking on the Android Open Source Project.

Updated on July 08, 2022

Comments

  • gsgx
    gsgx almost 2 years

    I want to set the minimum date the user can choose in a DatePicker to the current date. I've tried this:

    DatePicker datePicker = (DatePicker) findViewById(R.id.event_date);
    datePicker.setMinDate(System.currentTimeMillis());
    

    That gives me the following exception:

    12-01 12:23:31.226: E/AndroidRuntime(10311): Caused by: java.lang.IllegalArgumentException: fromDate: Sat Dec 01 12:23:31 EST 2012 does not precede toDate: Sat Dec 01 12:23:31 EST 2012
    

    How do I do this?