How to set the limit on date in Date picker dialog

29,075

Solution 1

You have the setMinDate(long) and setMaxDate(long) methods at your disposal. Both of these will work on API level 11 and above. Since you are using a DatePickerDialog, you need to first get the underlying DatePicker by calling the getDatePicker() method.

dpdialog.getDatePicker().setMinDate(minDate);  
dpdialog.getDatePicker().setmaxDate(maxDate);  

Source :Set Limit on the DatePickerDialog in Android?

You can calculate the minDate by using,

Date today = new Date();
Calendar c = Calendar.getInstance();
c.setTime(today);
c.add( Calendar.MONTH, -6 ) // Subtract 6 months
long minDate = c.getTime().getTime() // Twice!  

Updated :

Replace the below line

return new DatePickerDialog(getActivity(), this, year, month, day);

with

 // Create a new instance of DatePickerDialog and return it
    DatePickerDialog pickerDialog = new DatePickerDialog(getActivity(), this, year, month, day);
    pickerDialog.getDatePicker().setMaxDate(maxDate);
    pickerDialog.getDatePicker().setMinDate(minDate);
    return pickerDialog;

Solution 2

In some cases, when you set maximum date with a date without hours and minutes, you won't be able to select the maximum date you set. For instance

 Calendar maxDate = Calendar.getInstance();
            maxDate.set(Calendar.DAY_OF_MONTH, day + 5);
            maxDate.set(Calendar.MONTH, month);
            maxDate.set(Calendar.YEAR, year);

 datePickerDialog.getDatePicker().setMaxDate(maxDate.getTimeInMillis());

With the code block above, you can not click to your maxDate.

But if you add hours and minutes to like;

            maxDate.set(Calendar.HOUR, 23);
            maxDate.set(Calendar.MINUTE, 59);

to your maxDate, your last date will be clickable

Solution 3

by using

setMinDate and setMaxDate

MinDate

MaxDate

Share:
29,075
stacy queen
Author by

stacy queen

Updated on July 21, 2022

Comments

  • stacy queen
    stacy queen almost 2 years

    I want to put the limit on date so that user can not pick date more then that, for example if today is 1 January then User should not be able to select more then 7 dates , I mean he can not select 9 January. I also want him not to select the month and year. So I am putting a limit to set his task in one week.

    what I have done so far is showing the date picker fragment and setting current date in it. the code in my main activity goes like this:

    etSelectDate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DialogFragment datePickerFragment = new DatePickerFragment() {
                        @Override
                        public void onDateSet(DatePicker view, int year, int month, int day) {
                            Log.d("Date Change", "onDateSet");
                            Calendar c = Calendar.getInstance();
                            c.set(year, month, day);
                            DateFormat df = DateFormat.getDateInstance();
                            etSelectDate.setText(df.format(c.getTime()));
                            //nextField.requestFocus(); //moves the focus to something else after dialog is closed
    
                        }
                    };
                    datePickerFragment.show(MainActivity.this.getSupportFragmentManager(), "datePicker");
    
                }
            });
    

    and date picker fragment class goes like this :

     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
                return new DatePickerDialog(getActivity(), this, year, month, day);
            }
    
            @Override
            public void onDateSet(DatePicker view, int year, int month, int day) {
                //blah
            }
        }
    

    till then its is working fine , but I do not know how to put the limit on date and rest of the months and year should be non Select able . I have seen many link such as like this , but I do not understand How can I do that and also there is nothing helpful on android site.

    So please help me , How can I put limit of seven days only

    Update

    Through your replies I know how to set the max date in calender , so As I want to set the max date 7 days ahead of current date , I am still not getting it. the method I read so far is :

    pickerDialog.getDatePicker().setMaxDate(new Date().getTime());
    

    It is setting the current date as maximum, but How can I add 7 days ahead in it since it is Date object ? please help

  • stacy queen
    stacy queen about 9 years
    can you please elaborate it more. where I can use this and in which format
  • stacy queen
    stacy queen about 9 years
    I have seen this thread earlier but How can I implement in my case
  • Kartheek
    Kartheek about 9 years
    @stacyqueen Updated my answer, have a look
  • stacy queen
    stacy queen about 9 years
    so how can I put limit of seven days in Max date ? and min date would be cureent date for sure ?
  • stacy queen
    stacy queen about 9 years
    Your solution helped me a lot , but there is one problem in it
  • Kartheek
    Kartheek about 9 years
    @stacyqueen What is the problem?
  • stacy queen
    stacy queen about 9 years
  • hasnain_ahmad
    hasnain_ahmad about 7 years
    After setting the setMaxDate of datePicker the setMaxDate is always highlighted and selected, here is the image