简体   繁体   中英

Android JAVA Google Material Date Picker Disable Future Dates

I need to make sure the user doesn't pick future dates as selection to filter records. I am able to restrict months, but can't restrict future dates. please help.

Here is the code I use:

// create the instance of the calendar to set the
    // bounds
    Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));

    // now set the starting bound from current month to
    // previous MARCH
    /*calendar.set(Calendar.MONTH, Calendar.MARCH);
    long march = calendar.getTimeInMillis();*/

    // now set the ending bound from current month to
    // DECEMBER
    //calendar.set(Calendar.MONTH, Calendar.DECEMBER);
    //long december = calendar.getTimeInMillis();

    calendar.set(Calendar.DATE, Calendar.DAY_OF_MONTH);
    long currentDateTimeMills = calendar.getTimeInMillis();

    // create the instance of the CalendarConstraints
    // Builder
    CalendarConstraints.Builder calendarConstraintBuilder = new CalendarConstraints.Builder();

    // and set the start and end constraints (bounds)
    //calendarConstraintBuilder.setStart(march);
    //calendarConstraintBuilder.setEnd(december);
    
    calendarConstraintBuilder.setEnd(currentDateTimeMills);

    MaterialDatePicker.Builder<Pair<Long, Long>> materialDateBuilder = MaterialDatePicker.Builder.dateRangePicker();

    materialDateBuilder.setTitleText("SELECT A DATE");

    materialDateBuilder.setCalendarConstraints(calendarConstraintBuilder.build());

    //final MaterialDatePicker<Pair<Long, Long>> materialDatePicker = materialDateBuilder.build();
    final MaterialDatePicker materialDatePicker = materialDateBuilder.build();
    dateRangePickerButton.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(fragmentManager.findFragmentByTag("MATERIAL_DATE_PICKER") == null) {
                        materialDatePicker.show(((FragmentActivity) v.getContext()).getSupportFragmentManager(), "MATERIAL_DATE_PICKER");
                    }
                }
            });
DatePickerDialog dialog = new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay);
dialog.getDatePicker().setMaxDate(new Date().getTime());
return dialog;

Add this line

    dialog.datePicker.maxDate = Date().time

All thanks to - https://www.geeksforgeeks.org/more-functionalities-of-material-design-date-picker-in-android/?ref=lbp

i was able to disable future results using the below code:

CalendarConstraints.Builder calendarConstraintBuilder = new CalendarConstraints.Builder();

    calendarConstraintBuilder.setValidator(DateValidatorPointBackward.now());

    MaterialDatePicker.Builder<Pair<Long, Long>> materialDateBuilder = MaterialDatePicker.Builder.dateRangePicker();

    materialDateBuilder.setTitleText("SELECT DATE RANGE");

    materialDateBuilder.setCalendarConstraints(calendarConstraintBuilder.build());

    //final MaterialDatePicker<Pair<Long, Long>> materialDatePicker = materialDateBuilder.build();
    final MaterialDatePicker materialDatePicker = materialDateBuilder.build();
    dateRangePickerButton.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(fragmentManager.findFragmentByTag("MATERIAL_DATE_PICKER") == null) {
                        materialDatePicker.show(((FragmentActivity) v.getContext()).getSupportFragmentManager(), "MATERIAL_DATE_PICKER");
                    }
                }
            });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM