60

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?

gsgx
  • 12,020
  • 25
  • 98
  • 149

11 Answers11

173

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

Sam
  • 86,580
  • 20
  • 181
  • 179
  • 1
    apart from above solution, you can simply set the minimum values for that particular date as mentioned [here](http://stackoverflow.com/a/23006624/1288725) – Mehul Joisar Nov 21 '14 at 07:20
  • 1
    This solution partially works for me on 5.0.1 (Samsung S4) with `DatePickerDialog`. I can **still** select any date in the current month. However, I can't select a date from a previous month. – Shubham A. Aug 31 '15 at 05:15
  • Can also use: setMinDate(Calendar.getInstance().getTimeInMillis()); – ITJscott Jun 13 '16 at 13:39
  • @ShubhamA Did you solve this issue because it happened with me too. I can select any date in the current month. – Khalid Ali Jan 24 '18 at 07:47
  • **- 1000** doesn't work for Kotlin for some cases :( – J A S K I E R Jan 31 '21 at 13:46
18

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

datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis()); 
Karl Gjertsen
  • 4,690
  • 8
  • 41
  • 64
Nikhil Jain
  • 235
  • 2
  • 4
  • This doesn't work for devices below 5.0 for some reason. The marked answer works for all the cases. – Ramesh Dec 06 '16 at 05:19
  • This will result in the same exception as the OP described (at least on Android API 16 and 18) and it's just a different way to select the date picker. – coffeemakr May 12 '18 at 20:05
8

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 
Saiful Alam
  • 121
  • 2
  • 3
8

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();
Satyajit
  • 625
  • 7
  • 12
4

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

datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis());
Mahmoud Ayman
  • 1,157
  • 1
  • 17
  • 27
TechInsect
  • 124
  • 1
  • 2
3

For custom date

Calendar c = Calendar.getInstance();
c.set(2017, 0, 1);//Year,Month -1,Day
your_date_picker.setMaxDate(c.getTimeInMillis());
Navin Kumar
  • 3,393
  • 3
  • 21
  • 46
2
datePicker.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);

The minimum date can't be the instant moment

0

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);
Ali Imran
  • 8,927
  • 3
  • 39
  • 50
  • I don't want to set the default date I want to set the minimum date the user can choose. – gsgx Dec 01 '12 at 17:32
0

Try this

datePicker.setMinDate(new GregorianCalendar.getTimeInMillis());
Robin Chander
  • 7,225
  • 3
  • 28
  • 42
0
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();
Brinda Rathod
  • 2,693
  • 1
  • 20
  • 32
0

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

myDatePicker.setMinDate(System.currentTimeMillis())