0

I have the following function which displays a DatePicker, for which I am trying to set the min and max dates that are allowed. I am successfully setting the max date to the date I am after, but I am having no luck with the min date. I want the min date to be set to tomorrow, so that the user cannot select today's date in the dialog.

What am I doing wrong?

    Calendar now = Calendar.getInstance();
    //now.add(Calendar.DAY_OF_MONTH, 1);
    Calendar c1 = Calendar.getInstance();
    c1.add(Calendar.DAY_OF_MONTH, 31);
    dateButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            DatePickerDialog datePickerDialog ;
            datePickerDialog = DatePickerDialog.newInstance(LoginSuccess.this,  now.get(Calendar.YEAR),
                    now.get(Calendar.MONTH),
                    now.get(Calendar.DAY_OF_MONTH));
            datePickerDialog.setThemeDark(false);
            datePickerDialog.showYearPickerFirst(false);
            datePickerDialog.setAccentColor(Color.parseColor("#009688"));
            datePickerDialog.setTitle("Select Date From DatePickerDialog");
            Calendar cal=Calendar.getInstance();
            cal.setTimeInMillis(System.currentTimeMillis()+(1000 * 60 * 60 * 24));
           // cal.setTimeInMillis(Calendar.DAY_OF_YEAR, 1);
            datePickerDialog.setMinDate(cal);

            now.add(Calendar.DAY_OF_MONTH, 30);
            List<Calendar> dayslist= new LinkedList<Calendar>();
            Calendar[] daysArray;
            Calendar cAux = Calendar.getInstance();
            while ( cAux.getTimeInMillis() <= now.getTimeInMillis())
            {
                if (cAux.get(Calendar.DAY_OF_WEEK)!=1)
                {
                    Calendar c = Calendar.getInstance();
                    c.setTimeInMillis(cAux.getTimeInMillis());
                    dayslist.add(c);
                }
                cAux.setTimeInMillis(cAux.getTimeInMillis()+(24*60*60*1000));
            }
            daysArray = new Calendar[dayslist.size()];
            for (int i = 0; i<daysArray.length;i++)
            {
                daysArray[i]=dayslist.get(i);
            }
            datePickerDialog.setMaxDate(c1);
            datePickerDialog.show(getFragmentManager(), "DatePickerDialog");
            datePickerDialog.setSelectableDays(daysArray);

        }
    });
}
Adrian Sanguineti
  • 2,455
  • 1
  • 27
  • 29
aenugula karthik
  • 339
  • 5
  • 19

4 Answers4

2

Implement like this,

  Calendar now = Calendar.getInstance();
                DatePickerDialog dpd = DatePickerDialog.newInstance(
                        CreateAuction_Activity.this,
                        now.get(Calendar.YEAR),
                        now.get(Calendar.MONTH),
                        now.get(Calendar.DAY_OF_MONTH)
                );
                dpd.show(getFragmentManager(), "Datepickerdialog");
                now.add(Calendar.DAY_OF_MONTH, +1);// This the line you are asking
                dpd.setMinDate(now);
Vadivel
  • 780
  • 1
  • 6
  • 21
0

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

Binesh Kumar
  • 2,763
  • 1
  • 16
  • 23
0
final Calendar cal = Calendar.getInstance();
day = cal.get(Calendar.DAY_OF_MONTH);
tomorrow=day+1;
tomorrow.setTimeInMillis(System.currentTimeMillis()+(1000 * 60 * 60 * 24));
datePickerDialog.setMinDate(tomorrow);
babiro
  • 95
  • 2
  • 11
0

Use this code for select tomorrow date. Just tested in material design datepicker

int mYear,mMonth,mDay;
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog=new DatePickerDialog(context, R.style.datepicker,new DatePickerDialog.OnDateSetListener() {
   @Override
   public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
       ETDate.setText(dayOfMonth + "-" + (month + 1) + "-" + year);
   }
}, mYear, mMonth, mDay);
datePickerDialog.getDatePicker().setMinDate(c.getTimeInMillis()+(1000*24*60*60));
datePickerDialog.show();

my style for DATEPICKER . customize as you want...

<style name="datepicker" parent="@android:style/Theme.Holo.Light.Dialog">
    <!---TODO-->
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:focusedMonthDateColor">@android:color/holo_red_dark</item>
</style>

Update update style as your github library dtpicker

<style name="datepicker" parent="@android:style/Theme.DeviceDefault.Light.Dialog">
    <!---TODO-->
    <item name="android:focusedMonthDateColor">@android:color/holo_red_dark</item>
</style>
Abhishek Singh
  • 9,008
  • 5
  • 28
  • 53