1

I created my date picker using this link Date Picker

I know we can use setMinDate() method to set Minimum date but I don't understand how to do it if we implement date picker the way it is provided in the link. I didn't find any way of doing it while using this method. Date Picker code

public static class DatePickerFragment extends DialogFragment
        implements DatePickerDialog.OnDateSetListener {

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        tv_date= (TextView)getActivity().findViewById(R.id.tv_date);
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        // Do something with the date chosen by the user
        tv_date.setText(new StringBuilder().append(day).append("-")
                .append(month+1).append("-").append(year));
        date=tv_date.getText().toString();
    }
}
Vivek Mishra
  • 5,669
  • 9
  • 46
  • 84

2 Answers2

2

As you can see in this link : DatePicker#setMinDate , We need to get date picker object to set min or max date in the picker dialog.

See code snippet given below, which will solve your issue:

public static class DatePickerFragment extends DialogFragment
                            implements DatePickerDialog.OnDateSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        // Create a new instance of DatePickerDialog and return it
        DatePickerDialog dialog = new DatePickerDialog(context, this, year, month, day);

        //set min date (ex. current date and time) - pass your custom date and time in milliseconds
        dialog.getDatePicker().setMinDate(c.getTimeInMillis());
        //set max date
        //dialog.getDatePicker().setMaxDate(maxDate);

        return dialog;
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        // Do something with the date chosen by the user
    }
}

Hope it will help you.

Harin
  • 2,413
  • 3
  • 15
  • 30
1

Use this code:

final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);

DatePickerDialog d = new DatePickerDialog(getActivity(), listener,    year, month, day);  
DatePicker dp = d.getDatePicker(); 
dp.setMinDate(c.getTimeInMillis());
return d;
Rohit Sharma
  • 2,017
  • 1
  • 20
  • 22