title: Set Limit on the DatePickerDialog in Android?
body: <p>How to limit on DatePickerDialog to choose a date from 2-12 years in the past or from 0-2 years from the current date? </p>
  
  <p>I have DatePickerDialog in activity which is used for getting the date of birth for child and infant. Child age will be (2-12yrs) and infant age can be (0-2ys). If I pass 1 then the age limit should be (2-12yrs) and if I pass 2 the age limit should be (0-2yrs).</p>
  
  <p>What could I do to get this functionality in my calendar? </p>
  
  <p>My <strong>Calender code</strong> is:</p>
  
  <pre><code>date = 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);
                  String myFormat = "dd/MM/yy"; //In which you need put here
                  SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
                  dateofBirth.setText(sdf.format(myCalendar.getTime()));
              }
          };
  
  dateofBirth.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
                  showDialog(DATE_DIALOG_ID);
  
              }
          });
  
  public Dialog onCreateDialog(int id) {
      final Calendar now = Calendar.getInstance();
  
      switch (id) {
      case DATE_DIALOG_ID:
          // set date picker as current date
          DatePickerDialog _date =   new DatePickerDialog(this, date,myCalendar
                  .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                  myCalendar.get(Calendar.DAY_OF_MONTH)){
              @Override
  
              public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth){   
  
                  if (year &gt; now.get(Calendar.YEAR))
  
                      view.updateDate(myCalendar
                              .get(Calendar.YEAR), myCalendar
                              .get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH));
  
                  if (monthOfYear &gt; now.get(Calendar.MONTH) &amp;&amp; year == now.get(Calendar.YEAR))
                      view.updateDate(myCalendar
                              .get(Calendar.YEAR), myCalendar
                              .get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH));
  
                  if (dayOfMonth &gt; now.get(Calendar.DAY_OF_MONTH) &amp;&amp; year == now.get(Calendar.YEAR) &amp;&amp; 
                          monthOfYear == now.get(Calendar.MONTH))
                      view.updateDate(myCalendar
                              .get(Calendar.YEAR), myCalendar
                              .get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH));
              }
  
          };
  
          return _date;
      }
      return null;
  }
  </code></pre>