-2

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);
    }
};
  • Take the check-in date as a long variable in milliseconds and then set it to the minimum date of the check-out DatePicker. – vidulaJ Nov 03 '16 at 05:29
  • How to get the check-in dates as a long variable in milliseconds? Can show some example please ? – JackdonChew Nov 03 '16 at 05:33

2 Answers2

0

Then you should try this :-

datePickerDialog.getDatePicker().setMinDate(newDate.getTime()-(newDate.getTime()%(24*60*60*1000)));
LoveAndroid
  • 186
  • 1
  • 11
  • I have test both the two line , and it will only disable the pass date base on today. And this I have already done as stated at my question, the objective i want to achieve is: I have two DataPickerDialog, Check In and Check Out. I wish to disable all the pass date of CheckOut datepicker dialog base on the chosen date at CheckIn datepicker dialog. In details, I give a example: User Choose 15/11/2016 at CheckIn DatePicker Dialog. I wish to block the previous date base on 15/11/2016 at CheckOut dialog. – JackdonChew Nov 03 '16 at 05:40
  • @JackdonChew Please check my updated answer.if you have any query then you can ask me. – LoveAndroid Nov 03 '16 at 05:45
  • Date newDate = c.getTime(); Is it the newDate declaring like this ? – JackdonChew Nov 03 '16 at 05:51
  • @JackdonChew newDate is your checkout date. – LoveAndroid Nov 03 '16 at 06:19
  • what place should i declare the newDate – JackdonChew Nov 03 '16 at 06:31
0

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