How to set a date limit in the date picker dialog

I want to set a date limit so that the user cannot select a date more than this, for example, if today is January 1, then the User will not be able to select more than 7 dates, I mean that he can not select January 9, I also want so that he does not choose the month and year. Therefore, I set a limit to set its task in one week.

what I have done so far shows a date selection snippet and sets the current date in it. The code in my main action is as follows:

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 the date picker slice class is as follows:

 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
        }
    }

, , , . , , , , Android.

, , ,

, , , 7 , . , :

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

, 7 , Date? ,

+4
4

setMinDate(long) setMaxDate(long). API 11 . DatePickerDialog, DatePicker, getDatePicker().

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

: DatePickerDialog Android?

minDate,

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!  

:

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

 // 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;
+14

, , . ,

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

maxDate.

, ;

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

maxDate,

+8

using

setMinDate and setMaxDate

Mindate

Maxdate

+1
source

try it

DatePickerDialog dpDialog = new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay);
DatePicker datePicker = dpDialog.getDatePicker();

Calendar calendar = Calendar.getInstance();//get the current day
datePicker.setMaxDate(calendar.getTimeInMillis());//set the current day as the max date or put yore date in miliseconds.
datePicker.setMinDate(calendar.getTimeInMillis());//set the current day as the min date or put your date in mili seconds format
return dpDialog;
0
source

All Articles