Joseph
Joseph

Reputation: 55

Disable past date in DatePickerDialog

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();
}

Upvotes: 2

Views: 2730

Answers (3)

Vivek Mishra
Vivek Mishra

Reputation: 5705

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
}

}

Upvotes: 1

Twisha Kotecha
Twisha Kotecha

Reputation: 1104

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.

Upvotes: 3

Ajeett
Ajeett

Reputation: 812

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();
}

Upvotes: 1

Related Questions