androiddatepickermindate

Set minimum date in date picker selected from another date picker


I have two date picker one is arrival date and other is departure date. what i want is when user opens departure date picker it should have +1 day as the minimum date in this date picker dialog.

Here is what i am doing

This is my arrival date picker

final Calendar c = Calendar.getInstance();
            arivalYear = c.get(Calendar.YEAR);
            arivalMonth = c.get(Calendar.MONTH);
            arivalDay = c.get(Calendar.DAY_OF_MONTH);

            DatePickerDialog dpd = new DatePickerDialog(this,
                    new DatePickerDialog.OnDateSetListener() {

                        @Override
                        public void onDateSet(DatePicker view, int year,
                                              int monthOfYear, int dayOfMonth) {
                            arivalDay = year;
                            arivalYear = monthOfYear;
                            arivalMonth = dayOfMonth;

                            arivalDateTV.setText(dayOfMonth + "-"
                                    + (monthOfYear + 1) + "-" + year);

                        }
                    }, arivalYear, arivalMonth, arivalDay);
            dpd.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
            dpd.show();

Here is my departure date picker

DatePickerDialog dpd = new DatePickerDialog(this,
                    new DatePickerDialog.OnDateSetListener() {

                        @Override
                        public void onDateSet(DatePicker view, int year,
                                              int monthOfYear, int dayOfMonth) {
                            departureYear = year;
                            departureMonth = monthOfYear;
                            departureDay = dayOfMonth;

                            departureDateTV.setText(dayOfMonth + "-"
                                    + (monthOfYear + 1) + "-" + year);

                        }
                    }, arivalYear, arivalMonth, arivalDay);
            Date min = new Date();
            min.setDate(arivalDay);
            min.setMonth(arivalMonth);
            min.setYear(arivalYear);

            dpd.getDatePicker().setMinDate(min.getDate());
            dpd.show();

But here i can even select the dates before the arrival date. What i am doing wrong.

Thanx in advance.


Solution

  • You probably assigning wrong values to arivalDay, arivalMonth and arivalYear.

    Inside your arrival date picker, check your onDateSet() method.

    The following lines are probably incorrect (you are assigning the selected year to arivalDay, the selected month to arivalYear, and selected day to arivalMonth).

    arivalDay = year;
    arivalYear = monthOfYear;
    arivalMonth = dayOfMonth;
    

    Should be:

    arivalDay = dayOfMonth;
    arivalYear = year;
    arivalMonth = monthOfYear;
    

    Also setDate(int day) is deprecated.

    This method was deprecated in API level 1. Use Calendar.set(Calendar.DATE, day) instead.