物料DatePicker范围获取选择的范围日期

时间:2020-03-17 16:01:41

标签: java android datepicker material-components-android

我想使用物料日期选择器选择日期范围。 如何获得所选范围的值?

    long today = MaterialDatePicker.todayInUtcMilliseconds();

    calendar.setTimeInMillis(today);

    calendar.set(Calendar.MONTH, Calendar.JANUARY);
    long january = calendar.getTimeInMillis();

    calendar.set(Calendar.MONTH, Calendar.MARCH);
    long march = calendar.getTimeInMillis();

    calendar.set(Calendar.MONTH, Calendar.DECEMBER);
    long december = calendar.getTimeInMillis();

    //CalendarConstraints
    CalendarConstraints.Builder constraintBuilder = new CalendarConstraints.Builder();
    constraintBuilder.setValidator(new DateValidatorWeekdays());

    //MaterialDatePicker
    MaterialDatePicker.Builder builder = MaterialDatePicker.Builder.dateRangePicker();
    builder.setTitleText("Seleziona Date");
    //builder.setSelection(today);
    builder.setCalendarConstraints(constraintBuilder.build());
    final MaterialDatePicker materialDatePicker = builder.build();

我刚刚尝试了materialDatePicker.getHeaderText(),但是否想知道区别?

1 个答案:

答案 0 :(得分:0)

我是这样的:

Pair prendiRange =(Pair)materialDatePicker.getSelection();

            final Pair<Date, Date> rangeDate = new Pair<>(new Date(prendiRange.first), new Date(prendiRange.second));

            Date dataInizioRange = rangeDate.first;
            Date dataFineRange = rangeDate.second;