How to Hide past dates on DatePicker dialog ? I don't wanna allow user to select past dates
What would be the best way ? if i want to restrict user to select future dates only !
@Override
public void onCreate(Bundle savedInstanceState)
{
.................................................
editDate.setOnClickListener(new View.OnClickListener() {
@SuppressWarnings("deprecation")
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(DIALOG_DATE);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_DATE:
return new DatePickerDialog(this, new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
dateTime.set(year, monthOfYear, dayOfMonth);
editDate.setText(dateFormatter
.format(dateTime.getTime()));
}
}, dateTime.get(Calendar.YEAR),
dateTime.get(Calendar.MONTH),
dateTime.get(Calendar.DAY_OF_MONTH));
}
return null;
}
}
You can restrict the users from selecting a date within the particular range by specifying MinDate and MaxDate properties. The default value of MinDate property is 1/1/1920 and MaxDate property is 12/31/2120 . Dates that appears outside the minimum and maximum date range will be disabled (blackout).
the previous dates we need to set the minDate property of the date picker. if we set minDate:0 then it will disable all the previous dates. and we set input attribute min:current_date then it will disable all the previous dates.
The requirement is also to be able to use dates in the format "dd-MMM-yy". For example, in the first date field, the user selects 26-Nov-16. Then, when they click on the date picker for the second date field, the earliest date they are able to select is 26 Nov.
Here is a complete solution where you can use <apex:input type="date" with jQuery Datepicker where property minDate set to 0 (to show only future dates). I used a normal html <input type="text" to show the jQuery Datepicker and hide the <apex:input type="date" field.
You have to use setMinDate for minimum date and you can also use setMaxDate if you want to set maximum date.
Here is sample code for SetMinDate
for DatePickerDialog
.
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_DATE:
DatePickerDialog mDatePickerDialog = new DatePickerDialog(this, new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
dateTime.set(year, monthOfYear, dayOfMonth);
editDate.setText(dateFormatter.format(dateTime.getTime()));
}
}, dateTime.get(Calendar.YEAR), dateTime.get(Calendar.MONTH), dateTime.get(Calendar.DAY_OF_MONTH));
Calendar mCalendarMinimum = Calendar.getInstance();
mCalendarMinimum.add(Calendar.HOUR, -1);
mDatePickerDialog.getDatePicker().setMinDate(mCalendarMinimum.getTimeInMillis());
return mDatePickerDialog;
}
return null;
}
You can use setMinDate (long minDate) to achieve your goal. Doc says:
Sets the minimal date supported by this NumberPicker in milliseconds since January 1, 1970 00:00:00 in getDefault() time zone.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With