androidandroid-layoutandroid-fragmentsandroid-datepickermaterial-components

How count the number of days selected in the daterangepicker materialdesign?


Hello guys I have a DatePickerRange with material design and everything is working perfectly

  CalendarConstraints.Builder constraitBuilder = new CalendarConstraints.Builder();
        constraitBuilder.setStart(mesPresente);
        constraitBuilder.setEnd(limitDate);
       // selectedPair = new Pair<>(today,);  Selecionar da por default in DateRangePicker
        //Date Range Picker
        MaterialDatePicker.Builder<Pair<Long, Long>> builder = MaterialDatePicker.Builder.dateRangePicker();
        builder.setTitleText("Seleciona a data");
        //builder.setSelection(selectedPair);
        builder.setCalendarConstraints(constraitBuilder.build()); // Delemita calendario apenas no ano atual
        final MaterialDatePicker materialDatePicker = builder.build();

        txtCalendar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                materialDatePicker.show(getActivity().getSupportFragmentManager(),"DATE_PICKER");

            }
        });



        materialDatePicker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener() {
            @Override
            public void onPositiveButtonClick(Object selection) {
                txtCalendar.setText(materialDatePicker.getHeaderText())
            }
        });

Now I would very much like to count the number of days that were selected, that is, how many days were chosen?

At the moment I don't know how to get this information ... thank you for your help now ...


Solution

  • Thank you Gabriele Mariotti Thanks to your help, which was of great value, I managed to reach a solution, which

    materialDatePicker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener<Pair<Long, Long>>() {
      @Override public void onPositiveButtonClick(Pair<Long,Long> selection) {
          Long startDate = selection.first;
          Long endDate = selection.second;
    
          //count days....
           long numDays = endDate - startDate;
           int numDay = (int) TimeUnit.MILLISECONDS.toDays(numDias);
    
      }
    });