javaandroidmaterialdatepicker

DateRangePicker set limit one month before today


I'm new to android. Now, I want to set the calendar range just one month since today. I've tried this solution but I still don't get how to make it dynamic. And, I'm using Material Date Picker. Here's my code:

 private static Calendar getClearedUTC() {
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
        calendar.clear();
        return calendar;
    }

    private void initSetting() {
        today = MaterialDatePicker.todayInUtcMilliseconds();

        Calendar calendar = getClearedUTC();
        calendar.roll(Calendar.MONTH,  -1);
        monthBefore = calendar.getTimeInMillis();

        todayPair = new Pair<>(today, today);
        mothBeforePair = new Pair<>(monthBefore, monthBefore);
    }

    void onGroupClick(){
        initSetting();

        MaterialDatePicker.Builder<Pair<Long, Long>> builder = MaterialDatePicker.Builder.dateRangePicker();
        builder.setTitleText("Pilih tanggal");
        builder.setSelection(todayPair);
//        builder.setCalendarConstraints(limitRange().build());
        CalendarConstraints.DateValidator dateValidator = DateValidatorPointForward.from(today);
        final MaterialDatePicker materialDatePicker = builder.build();

        dateBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                materialDatePicker.show(getSupportFragmentManager(), "DATE_PICKER");
            }
        });
        materialDatePicker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener() {
            @Override
            public void onPositiveButtonClick(Object selection) {
                dateTV.setText(materialDatePicker.getHeaderText());
            }
        });
    }

Can someone help me?


Solution

  • Get today with: Calendar.getInstance()

    Add one month with: maxDate.add(Calendar.DAY_OF_MONTH, 30)

    Subtract one month with: maxDate.add(Calendar.DAY_OF_MONTH, -30)

    private void enableOneMonthSinceToday() {
        MaterialDatePicker.Builder<Long> builderRange = MaterialDatePicker.Builder.datePicker();
        builderRange.setCalendarConstraints(oneMonthBeforeTodayConstraints().build());
        MaterialDatePicker<Long> pickerRange = builderRange.build();
        pickerRange.show(getSupportFragmentManager(), pickerRange.toString());
    }
    
    private CalendarConstraints.Builder oneMonthBeforeTodayConstraints() {
        CalendarConstraints.Builder constraintsBuilderRange = new CalendarConstraints.Builder();
    
        Calendar maxDate = Calendar.getInstance();
        Calendar minDate = Calendar.getInstance();
        minDate.add(Calendar.DAY_OF_MONTH, -30); // subtracting 30 days
    
        constraintsBuilderRange.setStart(minDate.getTimeInMillis());
        constraintsBuilderRange.setEnd(maxDate.getTimeInMillis() );
    
        constraintsBuilderRange.setValidator(new RangeValidator(minDate.getTimeInMillis(), maxDate.getTimeInMillis()));
    
        return constraintsBuilderRange;
    
    }
    

    And validate the range with min and max values with RangeValidator

    static class RangeValidator implements CalendarConstraints.DateValidator {
    
        long minDate, maxDate;
    
        RangeValidator(long minDate, long maxDate) {
            this.minDate = minDate;
            this.maxDate = maxDate;
        }
    
        RangeValidator(Parcel parcel) {
        }
    
        @Override
        public boolean isValid(long date) {
            return !(minDate > date || maxDate < date);
        }
    
        @Override
        public int describeContents() {
            return 0;
        }
    
        @Override
        public void writeToParcel(Parcel dest, int flags) {
    
        }
    
        public static final Parcelable.Creator<RangeValidator> CREATOR = new Parcelable.Creator<RangeValidator>() {
    
            @Override
            public RangeValidator createFromParcel(Parcel parcel) {
                return new RangeValidator(parcel);
            }
    
            @Override
            public RangeValidator[] newArray(int size) {
                return new RangeValidator[size];
            }
        };
    
    
    }
    

    Usage:

    enableOneMonthSinceToday();