Newbie Progie Newbie Progie - 1 month ago 13
Android Question

Android studio: DatePickerDialog cant setMaxDate to 1998

I'm making a registration form with a birthdate detail. But I need it to be set it as maximum of 18 years ago. But when I

view.setMaxDate(1998);
nothing happens on the first set, but on second set the max date is
1970
in the dialog. Here is my code guys.

Initialize calendar



final Calendar cal = Calendar.getInstance();
year_x = 1998;
month_x = cal.get(Calendar.MONTH);
day_x = cal.get(Calendar.DAY_OF_MONTH);


the DatePickerDialog



public void showDialogOnButtonClick(){
etBirthDate = (EditText)findViewById(R.id.etBirthDate);

etBirthDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DIALOG_ID);
}
});
}

@Override
protected Dialog onCreateDialog(int id) {
if(id == DIALOG_ID) {
return new DatePickerDialog(this, dpListener, year_x, month_x, day_x);
}
return null;
}

DatePickerDialog.OnDateSetListener dpListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
view.setMaxDate(1998);
year_x = year;
month_x = monthOfYear + 1;
day_x = dayOfMonth;
etBirthDate.setText(month_x + "/" + day_x + "/" + year_x);
}
};

Answer

This seems to be wrong. This method of DatePicker needs Epoch. Pls set something like this and it will work.

//view.setMaxDate(1998);
view.setMaxDate(1546210800000L);

So in your case do this

view.setMaxDate(883612800000); // this will set date as 1st Jan 1998. Change the time accordingly.
Comments