【问题标题】:How to get date between start date & end date in material date range picker [duplicate]如何在材料日期范围选择器中获取开始日期和结束日期之间的日期[重复]
【发布时间】:2021-04-20 02:01:38
【问题描述】:

使用材料日期范围选择器时,我只能获取开始日期和结束日期,是否可以获取开始日期和结束日期之间的日期?如果可能的话,我怎么能做到这一点?

我正在查看 material.io 中的文档,

【问题讨论】:

标签: android date material-components-android


【解决方案1】:

没有任何方法可以做到。但是您可以使用 Calander 类来做到这一点 诀窍是为start 日期和另一个end 日期创建两个日历实例,使用while 循环,您可以通过添加一天来检查开始date 是否在end 日期之前start 日期每个 loop iteration

这是一个示例,我如何将日期保存为 long

 ArrayList<Long> dates = new ArrayList<>(); // the list to store all the dates 
 MaterialDatePicker.Builder<Pair<Long, Long>> builder = MaterialDatePicker.Builder.dateRangePicker();
 builder.setTitleText("Select rang");
 MaterialDatePicker<Pair<Long, Long>> materialDatePicker = builder.build();
 materialDatePicker.show(getSupportFragmentManager(), "Test");
 materialDatePicker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener<Pair<Long, Long>>() {
            @Override
            public void onPositiveButtonClick(Pair<Long, Long> selection) {
                if (selection.first != null && selection.second != null) {
                    // start date
                    Calendar start = Calendar.getInstance();
                    start.setTimeInMillis(selection.first);
                    // end date 
                    Calendar end = Calendar.getInstance();
                    end.setTimeInMillis(selection.second);
                    
                    while (start.before(end)) {
                        start.add(Calendar.DAY_OF_MONTH, 1); // add one day 
                        Log.d(TAG, "onPositiveButtonClick: " + start.get(Calendar.DAY_OF_MONTH));// show all the day between end and start  
                        dates.add(start.getTimeInMillis());
                    }
                }
            }
        });

有关详细信息,请参阅 Calander 类的 Field Manipulation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-04
    • 1970-01-01
    • 2019-07-04
    • 2019-04-23
    • 1970-01-01
    • 2015-10-18
    相关资源
    最近更新 更多