2

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();
}
Joseph
  • 55
  • 6
  • 2
    Does this answer your question? [How to disable past dates in Android date picker?](https://stackoverflow.com/questions/23762231/how-to-disable-past-dates-in-android-date-picker) – Ali Feb 18 '20 at 09:07
  • @Ali No, because i want to do this in my form..... – Joseph Feb 18 '20 at 09:37

3 Answers3

3

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.

Twisha Kotecha
  • 1,082
  • 1
  • 4
  • 18
  • I want my users to select next month dates, so my question is how to disable current month in datepickerdialog? – MaKi Jun 24 '21 at 03:05
1

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
}

}

Vivek Mishra
  • 5,669
  • 9
  • 46
  • 84
1

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();
}
Ajeett
  • 814
  • 2
  • 7
  • 18