How to set minimum DatePicker date to current date

AndroidAndroid Datepicker

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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 - Android

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

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


 

Solution 3 - Android

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 4 - Android

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 5 - Android

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

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

Solution 6 - Android

For custom date

Calendar c = Calendar.getInstance();
c.set(2017, 0, 1);//Year,Month -1,Day
your_date_picker.setMaxDate(c.getTimeInMillis());

Solution 7 - Android

This can help you.

Calendar cal=Calendar.getInstance();

int year=cal.get(Calendar.YEAR);
int month=cal.get(Calendar.MONTH);
int day=cal.get(Calendar.DAY_OF_MONTH);
int hour=cal.get(Calendar.HOUR_OF_DAY);
int min=cal.get(Calendar.MINUTE);

datePicker.updateDate(year, month, day);

timePicker.setCurrentHour(hour);
timePicker.setCurrentMinute(min);

Solution 8 - Android

Try this

datePicker.setMinDate(new GregorianCalendar.getTimeInMillis());

Solution 9 - Android

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

OR

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

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

Solution 10 - Android

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

The minimum date can't be the instant moment

Solution 11 - Android

To set the current date In Kotlin do it this way:

myDatePicker.setMinDate(System.currentTimeMillis())

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiongsingh2011View Question on Stackoverflow
Solution 1 - AndroidSamView Answer on Stackoverflow
Solution 2 - AndroidNikhil JainView Answer on Stackoverflow
Solution 3 - AndroidSaiful AlamView Answer on Stackoverflow
Solution 4 - AndroidSatyajitView Answer on Stackoverflow
Solution 5 - AndroidTechInsectView Answer on Stackoverflow
Solution 6 - AndroidNavin KumarView Answer on Stackoverflow
Solution 7 - AndroidAli ImranView Answer on Stackoverflow
Solution 8 - AndroidRobin ChanderView Answer on Stackoverflow
Solution 9 - AndroidBrinda RathodView Answer on Stackoverflow
Solution 10 - AndroidPierre AlexandreView Answer on Stackoverflow
Solution 11 - AndroidThe_Long_Distance_RunnerView Answer on Stackoverflow