I know there are quite a lot of question for this but none of the solutions are working for me, so this question. I want to restrict user to select date before today, but am
In the XML File
<DatePicker
android:minDate="mm/dd/yyyy"
android:maxDate="mm/dd/yyyy" >
ex:
android:minDate="10/12/2000"
android:maxDate="10/12/2000"
dialog.getDatePicker().setMinDate(calendar.getTimeInMillis());
I set the minimum date using the method to the current year which is 2020-05-20
like this
datePickerDialog.getDatePicker().setMinDate(calendar.getTimeInMillis());
The moment you click the date picker it displays 2020 as the minimum year
I hope the answer helps.
Above API 11 For min date you can use
android:minDate="mm/dd/yyyy"
or setMinDate(long time)
For Max date
android:maxDate="mm/dd/yyyy"
// In XML
setMaxDate(new Date().getTime());
// Current time
Get System Time and subtract 1 second from It
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
dialog = new DatePickerDialog(act, listener, year, month, day);
dialog.getDatePicker().setMinDate(System.currentTimeMillis() -1000);
return dialog;
}
Try this method
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(getContext(), listener, year, month, day);
Field mDatePickerField;
try {
mDatePickerField = dialog.getClass().getDeclaredField("mDatePicker");
mDatePickerField.setAccessible(true);
} catch (Exception e) {
e.printStackTrace();
}
dialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
return dialog;
}
instead of your
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(getContext(), listener, year, month, day);
dialog.getDatePicker().setMinDate(calendar.getTimeInMillis());
return dialog;
}
EDIT1:
I have also faced this issue that user can select not-selectable dates in Android L 5.0.2
. Currently there is bug reported here. It is solved in Android L 5.1.0
.
For temporary solution of this issue you can compare selected date with current system date and put some condition based on that. I used this as my workaround
EDIT2:
add onDateSent()
method in DatePickerDialogFragment
and just check if it's earlier than the date you set in setMinDate()
. If so, then just show the DatePickerDialog
again.
final long today = System.currentTimeMillis() - 1000;
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, monthOfYear, dayOfMonth);
//If user tries to select date in past (or today)
if (calendar.getTimeInMillis() < today)
{
//Make them try again
DatePickerDialogFragment fragment = new DatePickerDialogFragment();
fragment.setListener(dateSetListener);
fragment.show(getSupportFragmentManager(), "Choose booking date");
Toast.makeText(this, "Invalid date, please try again", Toast.LENGTH_LONG).show();
}
else
{
//success
}
}