How to Disable future dates in Android date picker
Solution 1
Get the DatePicker
from DatePickerDialog
with getDatePicker()
. Set the max date to current date with setMaxDate()
:
mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());
Requires API level 11.
Solution 2
You can call getDatePicker().setMaxDate(long) on your DatePickerDialog to set today as your maximum date. You can update the function with the same name from the snippet you posted.
Note:: DatePickerDialog is the object that I referenced in the Android Docs from the link I posted.
@Override
protected Dialog onCreateDialog(int id) {
Calendar c = Calendar.getInstance();
int cyear = c.get(Calendar.YEAR);
int cmonth = c.get(Calendar.MONTH);
int cday = c.get(Calendar.DAY_OF_MONTH);
switch (id) {
case DATE_DIALOG_ID:
//start changes...
DatePickerDialog dialog = new DatePickerDialog(this, mDateSetListener, cyear, cmonth, cday);
dialog.getDatePicker().setMaxDate(System.currentTimeMillis());
return dialog;
//end changes...
}
return null;
}
Try this and give your feedback!!!
Solution 3
Following code help you to disable future dates:
Declare calendar variable globally:
private Calendar myCalendar = Calendar.getInstance();
Put following code in onCreate method:
DatePickerDialog.OnDateSetListener dateListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
};
On the button click put the following code:
DatePickerDialog datePickerDialog=new DatePickerDialog(getActivity(), dateListener, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH));
//following line to restrict future date selection
datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis());
datePickerDialog.show();
Solution 4
// Custom Your Future Dates
// Example for today and next 3 days :- 3(NUMBER OF NEXT DAYS)*24*60*60*1000l
// As 24 represents hrs in day
// As 60 mins 60 secs and convert it to millisec
//Inside Class which implements DatePickerDialog.OnDateSetListener
private Calendar mCurrentDate;
//Inside OnCreate Method
mDateEditText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCurrentDate = Calendar.getInstance();
int year = mCurrentDate.get(Calendar.YEAR);
int month = mCurrentDate.get(Calendar.MONTH);
int day = mCurrentDate.get(Calendar.DAY_OF_MONTH);
DatePickerDialog mDatePickerDialog = new DatePickerDialog(this, this, year, month, day);
mDatePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis() + 3 * 24 * 60 * 60 * 1000 l);
}
});
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
mDateEditText.setText(dayOfMonth + "/" + month + "/" + year);
}
Solution 5
If user select future date then update datepicker to current date(today)
you can use following code to check selected is future date or not
final Calendar cal = Calendar.getInstance();
datePickerDob.init(currentYear, currentMonth, currentDay,
new OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
Calendar selectedCal = Calendar.getInstance();
selectedCal.set(year, monthOfYear, dayOfMonth);
long selectedMilli = selectedCal.getTimeInMillis();
Date datePickerDate = new Date(selectedMilli);
if (datePickerDate.after(new Date())) {
datePickerDob.updateDate(cal.get(Calendar.YEAR),
cal.get(Calendar.MONTH),
cal.get(Calendar.DAY_OF_MONTH));
} else {
}
}
});
You can also use compareTo() method
datePickerDate.compareTo(new Date());
Compare the receiver to the specified Date to determine the relative ordering.
Parameters date a Date to compare against.
Returns an int < 0 if this Date is less than the specified Date, 0 if they are equal, and an int > 0 if this Date is greater.
Comments
-
venu almost 2 years
How to Disable future dates in Android date picker
Java Code :
mExpireDate.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // To show current date in the datepicker final Calendar mcurrentDate = Calendar.getInstance(); int mYear = mcurrentDate.get(Calendar.YEAR); int mMonth = mcurrentDate.get(Calendar.MONTH); int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH); DatePickerDialog mDatePicker = new DatePickerDialog( EventRegisterActivity.this, new OnDateSetListener() { public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) { mcurrentDate.set(Calendar.YEAR, selectedyear); mcurrentDate.set(Calendar.MONTH, selectedmonth); mcurrentDate.set(Calendar.DAY_OF_MONTH, selectedday); SimpleDateFormat sdf = new SimpleDateFormat( getResources().getString( R.string.date_card_formate), Locale.US); mExpireDate.setText(sdf.format(mcurrentDate .getTime())); } }, mYear, mMonth, mDay); mDatePicker.setTitle(getResources().getString( R.string.alert_date_select)); mDatePicker.show(); } });
How to do it?
-
Saad Bilal about 10 yearswhere to add these lines ondatesetListner ??
-
Dut A. almost 10 years@SaadBilal, add these lines to the
onCreateDialog(int id){...}
after your dialog id case. -
Dut A. almost 10 years+1 for this. Instead of
.setMaxDate(new Date())
, it is better to instead use.setMaxDate(System.currentTimeMillis())
since the expected argument is along
. Otherwise, your approach here is elegant and simple. -
Ketan Ahir over 7 yearsThere is a bug in v5.0. setMaxDate() will grayed out date but still it can be selected.
-
Manikanta over 7 yearsThis solution may not work in some devices(future days of current month will be displayed). check my answer
-
Steve over 7 yearsthis answer is working good.disabled future date.But still it is clickable and select that date
-
Narendra Singh over 7 yearsIt also disabled current date along with the future dates. I didn't want to disable the current date
-
Rahul Hawge about 7 years@KetanAhir Ya i am also facing the same issue. Were you able to resolve it?
-
Keshav Gera almost 7 yearsmDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());
-
Keshav Gera almost 7 yearsmDatePicker.getDatePicker().setMinDate(System.currentTimeMillis());
-
Aditya Vyas-Lakhan almost 7 yearsI am using this github.com/wdullaer/MaterialDateTimePicker how can i disable future date?
-
laalto almost 7 years@AdityaVyas-Lakhan That library's
DatePickerDialog
has e.g.setMaxDate(Calendar)
. Post a new question if you need help with that. -
Amit Verma over 6 yearshow to implement this in DialogFragment implements DatePickerDialog.OnDateSetListener
-
Amit Verma over 6 yearsThanks I have implemented this in onCreateDialog // Create a new instance of DatePickerDialog and return it DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), this, year, month, day); datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis()); return datePickerDialog;