0

I have two calendar fields and I have two conditions to apply 1. The user is not allowed select the past date means app only allows the user to select a date from today only 2. The user is not allowed to select TO date that is before the from date

here is the code I am using

fromcal=Calendar.getInstance();
    tocal=Calendar.getInstance();

    final DatePickerDialog.OnDateSetListener fromdate = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {

            fromcal.set(Calendar.YEAR,year);
            fromcal.set(Calendar.MONTH,monthOfYear);
            fromcal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
            updateFromDate();

        }
    };

    fromtext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            new DatePickerDialog(RequestLeaveActivity.this,fromdate,
                    fromcal.get(Calendar.YEAR),fromcal.get(Calendar.MONTH),
                    fromcal.get(Calendar.DAY_OF_MONTH)).show();
        }
    });


    final DatePickerDialog.OnDateSetListener todate = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {

            tocal.set(Calendar.YEAR,year);
            tocal.set(Calendar.MONTH,monthOfYear);
            tocal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
            updateToDate();

        }
    };

    totext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            new DatePickerDialog(RequestLeaveActivity.this,todate,
                    tocal.get(Calendar.YEAR),tocal.get(Calendar.MONTH),
                    tocal.get(Calendar.DAY_OF_MONTH)).show();
        }
    });
Hemant Parmar
  • 3,924
  • 7
  • 25
  • 49
INONAME
  • 119
  • 1
  • 2
  • Possible duplicate of [Set Limit on the DatePickerDialog in Android?](https://stackoverflow.com/questions/18272306/set-limit-on-the-datepickerdialog-in-android) and of [How to set the limit on date in Date picker dialog](https://stackoverflow.com/questions/31049830/how-to-set-the-limit-on-date-in-date-picker-dialog) – Ole V.V. Jun 01 '18 at 09:20

2 Answers2

0

disable past date

datePicker.setMinDate(System.currentTimeMillis() - 1000);

Compare from and To dates

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date strDate = sdf.parse(valid_until);
if (new Date().after(strDate)) { 
    //print Toast
} 
0

You can achieve this using restricting user from selecting other dates. What I use was Times Square; a library for custom calendar.

 private void setupCalenderView() {
    Calendar nextYear = Calendar.getInstance();
    nextYear.add(Calendar.YEAR, 1);
    Date endDate = new DateTime().plusDays(7).toDate();    //End Date
    Date startDate = new Date();   //Start Date;

    cv_calender.init(startDate , endDate , Locale.ENGLISH)
            .withSelectedDate(new Date());
}

For Date Selection you have to set this listener - cv_calender.setOnDateSelectedListener(this); and implement these methods.

 @Override
public void onDateSelected(Date date) {
    String selectedDate = new SimpleDateFormat(Your_Date_Format, Locale.ENGLISH).format(date);
}

@Override
public void onDateUnselected(Date date) {
}
Ashutosh Sagar
  • 981
  • 8
  • 20