如何在 material 日期范围选择器中获取开始日期和结束日期之间的日期
How to get date between start date & end date in material date range picker
当使用 material 日期范围选择器时,我只能获取开始日期和结束日期,是否可以获取介于开始日期和结束日期之间的日期?如果可能,我该如何实现?
我正在查看 material.io、
中的文档
没有方法可以做到。但你可以用 Calander class
诀窍是为 start
日期和另一个 end
日期创建两个日历实例,使用 while
循环你可以检查开始 date
是否在 end
日期每个 loop iteration
.
在 start
日期上加一天
这是一个如何为我做的例子 我将日期保存为 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());
}
}
}
});
有关详细信息,请参阅 Field Manipulation of the Calander class
当使用 material 日期范围选择器时,我只能获取开始日期和结束日期,是否可以获取介于开始日期和结束日期之间的日期?如果可能,我该如何实现?
我正在查看 material.io、
中的文档没有方法可以做到。但你可以用 Calander class
诀窍是为 start
日期和另一个 end
日期创建两个日历实例,使用 while
循环你可以检查开始 date
是否在 end
日期每个 loop iteration
.
start
日期上加一天
这是一个如何为我做的例子 我将日期保存为 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());
}
}
}
});
有关详细信息,请参阅 Field Manipulation of the Calander class