when using material date range picker i can only get the start date & end date, is it possible to get date between start date & end date ? if it is possible how can i achieve that ?
i've looking at documentation in material.io,
when using material date range picker i can only get the start date & end date, is it possible to get date between start date & end date ? if it is possible how can i achieve that ?
i've looking at documentation in material.io,
There is no method that does. but you can do it with the Calander class
the trick is created two instances of calander for the start
date and another for the end
date, with a while
loop you can check if the start date
is before the end
date by adding a day to the start
date each loop iteration
.
here is an example how to do it for me I save the dates as a 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());
}
}
}
});
For more information see the Field Manipulation of the Calander class