Android Fragments 如何计算在日期范围内选择的天数?

quhf5bfb  于 11个月前  发布在  Android
关注(0)|答案(2)|浏览(104)

大家好,我有一个DatePickerRange与材料设计,一切都是完美的工作

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

字符串
我现在很想数一数,究竟是选择了多少天,即选择了多少天呢?
目前我不知道如何得到这些信息.

vql8enpb

vql8enpb1#

感谢Gabriele Mariotti感谢您的帮助,这是非常有价值的,我设法达成了一个解决方案,

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

  }
});

字符串

nszi6y05

nszi6y052#

更改您的OnPositiveButtonClickListener

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

  }
});

字符串

相关问题