Lorsque je défini:
datePicker.datePicker.minDate = System.currentTimeMillis()
3 Réponses :
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 dpd = new DatePickerDialog(getActivity(),AlertDialog.THEME_TRADITIONAL,this,year, month, day); //Get the DatePicker instance from DatePickerDialog DatePicker dp = dpd.getDatePicker(); //Set the DatePicker minimum date selection to current date dp.setMinDate(c.getTimeInMillis());//get the current day //dp.setMinDate(System.currentTimeMillis() - 1000);// Alternate way to get the current day //Add 6 days with current date c.add(Calendar.DAY_OF_MONTH,6); //Set the maximum date to select from DatePickerDialog dp.setMaxDate(c.getTimeInMillis()); //Now DatePickerDialog have 7 days range to get selection any one from those dates
Essayez ceci
Cette mise en œuvre serait bonne, mais elle utilise une bibliothèque externe comme date datchpicker. Je voulais utiliser celui que Android Studio fournit. :)
Essayez le code ci-dessous, cela devrait résoudre votre problème
Calendar newCalendar = Calendar.getInstance(); DatePickerDialog toDatePickerDialog = new DatePickerDialog(Record_Sample_Collection_Details.this, new DatePickerDialog.OnDateSetListener() { public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { Calendar newDate = Calendar.getInstance(); newDate.set(year, monthOfYear, dayOfMonth); } }, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH)); toDatePickerDialog.getDatePicker().setMinDate(newCalendar.getTimeInMillis());