如何在android日期选择器中获取日期



我使用material.io中的MaterialDatePicker。我能够创建日期选择器,并添加一个函数addOnPositiveButtonClickListener,当日期被选择。我想在函数中做的是获取datepicker中选择的日期。

但是onPositiveButtonClick只有一个参数selection,这是一个对象,在使用tostring或调试时记录它,我可以看到selection对象是一对有2个数字。我猜是日期的时间戳。但是我不能使用selection对分别获得这些值。

dateRangePicker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener() {
@Override
public void onPositiveButtonClick(Object selection) {
Log.i("selection",selection.toString()); // this prints """ selection "Pair{1655251200000 1657756800000}"  """
//selection.getKey() this function does not exist on the pair selection however
}

有人可以帮助如何从选择(pair/Object)中获得from和to日期,而不需要从toString()中切片和切割字符串

您可以这样重写onPositiveButtonClick方法:

@Override public void onPositiveButtonClick(Pair<Long,Long> selection) {
Long startDate = selection.first;
Long endDate = selection.second;
//Do something...
}

最新更新