Home » Java » java – Disable past date in DatePickerDialog-Exceptionshub

java – Disable past date in DatePickerDialog-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I want to add code .getDatePicker().setMinDate(newDate.getTime() - (newDate.getTime()%(24*60*60*1000)))for disable the past date but suddenly .show() cannot be used.

Can someone teach me how to solve this kind of problem?

Code:

btnDate.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            selectDate();
                        }
                    });

private void selectDate()
{
    DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
            calendar.set(Calendar.YEAR, year);
            calendar.set(Calendar.MONTH, month);
            calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);

            String selectedDate = dateFormat.format(calendar.getTime());

            btnDate.setText(selectedDate);
            btnDate.setTextColor(getResources().getColor(colorPrimaryDark));
        }
    };

    Date newDate = calendar.getTime();

    new DatePickerDialog(TutorAddNewTimeSlotActivity.this,  dateSetListener,
            calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();
}
How to&Answers:

First of all declare as following:

private Calendar myCalendar = Calendar.getInstance();

then like your code:-

DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);

        String selectedDate = dateFormat.format(calendar.getTime());

        btnDate.setText(selectedDate);
        btnDate.setTextColor(getResources().getColor(colorPrimaryDark));
    }
};

make this change in your code instead of your last two lines:

`Date newDate = calendar.getTime();

new DatePickerDialog(TutorAddNewTimeSlotActivity.this,  dateSetListener,
        calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();`

change it and make it like following:

datePickerDialog.getDatePicker().setMinDate(myCalendar.getTimeInMillis());
datePickerDialog.show();

which will disable past date.

Answer:

You can convert your method like this in order to set a minimum date

private void selectDate()
{
    DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
            calendar.set(Calendar.YEAR, year);
            calendar.set(Calendar.MONTH, month);
            calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);

            String selectedDate = dateFormat.format(calendar.getTime());

            btnDate.setText(selectedDate);
            btnDate.setTextColor(getResources().getColor(colorPrimaryDark));
        }
    };

    Date newDate = calendar.getTime();

DatePickerDialog dialog= new DatePickerDialog(this,  dateSetListener,
                calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();
dialog.getDatePicker().setMinDate(Calendar.getInstance().getTimeInMillis());
        dialog.show();
}

Answer:

You can simply do it like this

datePickerDialog.datePicker.minDate=Date().time

You can create your own date picker class by inheriting the default one and define the min date like the below code

class DatePicker(var onDateSetListener: DatePickerDialog.OnDateSetListener): DialogFragment() {
var date: Date? = null

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    val c = Calendar.getInstance()
    val year = c.get(Calendar.YEAR)
    val month = c.get(Calendar.MONTH)
    val day = c.get(Calendar.DAY_OF_MONTH)
    val datePickerDialog= DatePickerDialog(activity!!,onDateSetListener, year, month, day)
    datePickerDialog.datePicker.minDate=Date().time
    return datePickerDialog
}

}