Newbie Progie Newbie Progie - 1 year ago 108
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

nothing happens on the first set, but on second set the max date is
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(;

etBirthDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

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() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
year_x = year;
month_x = monthOfYear + 1;
day_x = dayOfMonth;
etBirthDate.setText(month_x + "/" + day_x + "/" + year_x);

Answer Source

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


So in your case do this

view.setMaxDate(883612800000); // this will set date as 1st Jan 1998. Change the time accordingly.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download