我尝试下面的代码,但我不能得到我想要的结果。
MaterialDatePicker.Builder<Pair<Long, Long>> builder = MaterialDatePicker.Builder.dateRangePicker();
builder.setTitleText("Select A Date");
final MaterialDatePicker<Pair<Long, Long>> materialDatePicker = builder.build();
date_picker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
materialDatePicker.show(getActivity().getSupportFragmentManager(), "DATE_PICKER");
}
});
materialDatePicker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener<Pair<Long, Long>>() {
@Override
public void onPositiveButtonClick(Pair<Long, Long> selection) {
Long start = selection.first;
Calendar start_cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
start_cal.setTimeInMillis(start);
SimpleDateFormat simpleDateFormat_start = new SimpleDateFormat("YYYY-MM-DD");
start_date = simpleDateFormat_start.format(start_cal.getTime());
Long end = selection.second;
Calendar end_cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
end_cal.setTimeInMillis(end);
SimpleDateFormat simpleDateFormat_end = new SimpleDateFormat("YYYY-MM-DD");
end_date = simpleDateFormat_end.format(end_cal.getTime());
String final_text = start_date + " to " + end_date;
date_picker.setText(final_text);
Log.e("Check65", "Date : " + final_text);
}
});
我想要的startdate可以:2021-01-01enddate: 2021-02-02
但是我得到startdate可以:2021-01-01enddate: 2021-02-32
在您的SimpleDateFormat
中,您应该使用YYYY-MM-dd
而不是YYYY-MM-DD
,因为大写DD表示一年中的一天,而您想要的是月份的一天。