在我的应用程序中,我希望用户选择去年的日期,例如从2016年1月1日到2016年12月31日,我尝试设置mindate& maxdate ....
public static class LastYearBirthDatePicker extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);
int lastYear = getCurrentYear()-1;
Calendar min = Calendar.getInstance();
min.set(Calendar.DAY_OF_MONTH,1);
min.set(Calendar.MONTH,0);
min.set(Calendar.YEAR,lastYear);
dialog.getDatePicker().setMinDate(min.getTimeInMillis());
Calendar max = Calendar.getInstance();
max.set(Calendar.DAY_OF_MONTH,31);
max.set(Calendar.MONTH,11);
max.set(Calendar.YEAR,lastYear);
dialog.getDatePicker().setMaxDate(max.getTimeInMillis());
return dialog;
}
public void onDateSet(DatePicker view, int year, int month, int day) {
}
}
但是有没有更好的选择来设置特定的年份 我也需要特定的月份,如2016年的任何一个月(用户可以选择特定月份的任何一天)
答案 0 :(得分:0)
您可以像这样设置
Calendar cal = Calendar.getInstance();
cal.set(2016, 01, 01);
cal.add(Calendar.YEAR, 1);
int xxday = cal.get(Calendar.DATE);
int xxmonth = cal.get(Calendar.MONTH);
int xxyear = cal.get(Calendar.YEAR);
datepicker.init(xxyear, xxmonth, xxday, null);