Android Datepickerdialog: Set Min and Max Date for Selection

How set maximum date in datepicker dialog in android?

Use setMaxDate().

For example, replace return new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay) statement with something like this:

    DatePickerDialog dialog = new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay);
dialog.getDatePicker().setMaxDate(new Date().getTime());
return dialog;

Android DatePickerDialog: Set min and max date for selection

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
}
}

Set MinDate and MaxDate in DatePIcker

To set the min date two years before and max two years after today use the following code:

Calendar c = Calendar.getInstance();
c.add(Calendar.YEAR, -2); // subtract 2 years from now
datePickerDialog.getDatePicker().setMinDate(c.getTimeInMillis());
c.add(Calendar.YEAR, 4); // add 4 years to min date to have 2 years after now
datePickerDialog.getDatePicker().setMaxDate(c.getTimeInMillis());

How to set a minimum and maximum limit for a date picker in android?

Try this, This shows from today to next 5 year date in datepicker dialog.

public class MainActivity extends Activity {

TextView txtDatePicker;
Calendar cal,cal1;
long maxDate;
Date date;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

cal = Calendar.getInstance();
cal1 = Calendar.getInstance();

txtDatePicker = (TextView) findViewById(R.id.txtDatePicker);
txtDatePicker.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
DatePickerDialog dialog = new DatePickerDialog(MainActivity.this, new OnDateSetListener() {

@Override
public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, ""+arg1+"/"+(arg2+1)+"/"+arg3, Toast.LENGTH_SHORT).show();
}
}, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
dialog.getDatePicker().setMinDate(System.currentTimeMillis());
cal1.add(Calendar.YEAR, 5);
dialog.getDatePicker().setMaxDate(cal1.getTimeInMillis());
dialog.show();
}
});
}
}

How to set the limit on date in Date picker dialog

You have the setMinDate(long) and setMaxDate(long) methods at your disposal. Both of these will work on API level 11 and above. Since you are using a DatePickerDialog, you need to first get the underlying DatePicker by calling the getDatePicker() method.

dpdialog.getDatePicker().setMinDate(minDate);  
dpdialog.getDatePicker().setmaxDate(maxDate);

Source :Set Limit on the DatePickerDialog in Android?

You can calculate the minDate by using,

Date today = new Date();
Calendar c = Calendar.getInstance();
c.setTime(today);
c.add( Calendar.MONTH, -6 ) // Subtract 6 months
long minDate = c.getTime().getTime() // Twice!

Updated :

Replace the below line

return new DatePickerDialog(getActivity(), this, year, month, day);

with

 // Create a new instance of DatePickerDialog and return it
DatePickerDialog pickerDialog = new DatePickerDialog(getActivity(), this, year, month, day);
pickerDialog.getDatePicker().setMaxDate(maxDate);
pickerDialog.getDatePicker().setMinDate(minDate);
return pickerDialog;

Set minimum date in date picker selected from another date picker

You probably assigning wrong values to arivalDay, arivalMonth and arivalYear.

Inside your arrival date picker, check your onDateSet() method.

The following lines are probably incorrect (you are assigning the selected year to arivalDay, the selected month to arivalYear, and selected day to arivalMonth).

arivalDay = year;
arivalYear = monthOfYear;
arivalMonth = dayOfMonth;

Should be:

arivalDay = dayOfMonth;
arivalYear = year;
arivalMonth = monthOfYear;

Also setDate(int day) is deprecated.

This method was deprecated in API level 1. Use
Calendar.set(Calendar.DATE, day) instead.



Related Topics



Leave a reply



Submit