Search code examples
androiddatedatepickerdurationperiod

Disable Previous dates base on selected date at other datepicker dialog


I would like to explain the problem in short, Now I have two android datapicker dialog, Check-In and Check-out. Check-in datepicker I have already set the mindate as today, which means user can only choose from today. But how to set the check-out date base on the date that been choosen by user from check-in datepicker.

Example: Today 3/11/2016

User choose from check-in DatePicker : 7/11/2016

I wish to disable all the previous date refer to 7/11/2016 at check-out Datepicker.

I searching many tutorial and forum, and most of the time only show how to disable pass date base on current date. My application is doing Hotel-Login System

public void showDialog() {
    btncheckin = (Button) findViewById(R.id.checkInDate);
    btncheckout = (Button) findViewById(R.id.checkoutdate);
    btncheckin.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showDialog(DIALOG_ID);

                }


            }
    );
    btncheckout.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showDialog(DIALOG2_ID);

                }


            }
    );

}

@Override
protected  Dialog onCreateDialog(int id) {

    switch (id) {
        case DIALOG2_ID:

            DatePickerDialog datePickerDialog = new DatePickerDialog(this, dpickerListnerCheckOut, year_x, month_x, day_x);


            Calendar c = Calendar.getInstance();

            c.add(Calendar.DATE, 1);

            Date newDateBlock = c.getTime();
            datePickerDialog.getDatePicker().setMinDate(newDateBlock.getTime());

            return datePickerDialog;

        case DIALOG_ID:

        DatePickerDialog datePickerDialog2 = new DatePickerDialog(this, dpickerListnerCheckIn, year_x, month_x, day_x);
            Calendar c2 = Calendar.getInstance();

            c2.add(Calendar.DATE, 1);
            datePickerDialog2.getDatePicker().setMinDate(System.currentTimeMillis()-1000);

            return datePickerDialog2;
    }
    return null;
}


 DatePickerDialog.OnDateSetListener dpickerListnerCheckOut = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
        year_x = year;
        month_x = month + 1;
        day_x = dayOfMonth;

         //Toast.makeText(PaymentActivity.this,year_x +"/"+month_x +" / "+day_x,Toast.LENGTH_SHORT).show();

        checkOutText.setText(year_x + "/" + month_x + "/ " + day_x);


    }
};


 DatePickerDialog.OnDateSetListener dpickerListnerCheckIn = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
        year_x = year;
        month_x = month + 1;
        day_x = dayOfMonth;
        checkInText.setText(year_x + "/" + month_x + "/ " + day_x);
    }
};

Solution

  • Inside my checkIn dpickerlistener I add the following line

    try {
         String dateString = checkInDate;
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
         Date date = sdf.parse(dateString);
         Long minDate = date.getTime();
         } catch (java.text.ParseException e) {
                e.printStackTrace();
         }
    

    At CheckOut Date Picker Dialog

    DatePickerDialog datePickerDialog = new DatePickerDialog(this, dpickerListnerCheckOut, year_x, month_x, day_x);
    Calendar c = Calendar.getInstance();
    
             c.add(Calendar.DATE, 1);
             datePickerDialog.getDatePicker().setMinDate(minDate);
    
    return datePickerDialog;
    

    The solution that i done is , i try to get mindate from checkin dpickerlistener minDate, and pass to checkout datepickerdialog the minDate