我正在开发一个我需要日期选择器对话框的android项目。 我已经完成了以下代码。
private void setDateTimeField() {
Calendar newCalendar = Calendar.getInstance();
firstMemberDatePickerDialog = new DatePickerDialog(getActivity(), new OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
BootstrapEditTextDob.setText(dateFormatter.format(newDate.getTime()));
}
},newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
}
现在,当用户打开日期选择器对话框时,它应该是18年前的过去日期。如何实现这个目标?
答案 0 :(得分:1)
要设置为过去日期,请使用以下代码
int no_of_years_past=18;
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
firstMemberDatePickerDialog = new DatePickerDialog(getActivity(), new OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
BootstrapEditTextDob.setText(dateFormatter.format(newDate.getTime()));
}
},year-no_of_years_past, month, day);
您可以使用相同的方法设置月份或日期