title: How to set a minimum and maximum limit for a date picker in android?
body: <p>I'm working on an app in which i'm using a custom date and time picker.
  I've created a dialog box and inside that i'm showing date and time picker respectively now I want to set a minimum and maximum limit for the date picker.
  Such as one should not be able to select the previous date from today and not more than a month ahead. 
  This is my code </p>
  
  <pre><code>  AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
              final DatePicker picker = new DatePicker(getActivity());
              try {
                  Field f[] = picker.getClass().getDeclaredFields();
                  for (Field field : f) {
                      if (field.getName().equals("mYearPicker") || field.getName().equals("mYearSpinner")) {
                          field.setAccessible(true);
                          Object yearPicker = new Object();
                          yearPicker = field.get(picker);
                          ((View) yearPicker).setVisibility(View.GONE);
                      }
                  }
              } catch (Exception ex) {
                  ex.printStackTrace();
              }
              picker.setCalendarViewShown(false);
              picker.setMinDate(new  Date().getTime());
  
              builder.setTitle("Please select date on which you would be leaving :")
                      .setView(picker)
                      .setPositiveButton("OK",
                              new DialogInterface.OnClickListener() {
                                  public void onClick(DialogInterface dialog, int whichButton) {
                                      SimpleDateFormat parseFormat = new SimpleDateFormat("EEE dd MMM");
                                      Date date1 = new Date();
                                      date1.setDate(picker.getDayOfMonth());
                                      date1.setMonth(picker.getMonth());
                                      final String s = parseFormat.format(date1);
                                      Log.e("DATE", s);
                                      SimpleDateFormat sdf=new SimpleDateFormat("MM-dd-yyyy");
                                      Date date2=new Date();
                                      date2.setDate(picker.getDayOfMonth());
                                      date2.setMonth(picker.getMonth());
                                      final String s2=sdf.format(date2);
  
  
                                      //Time picker
                                      AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                                      final TimePicker picker = new TimePicker(getActivity());
                                      picker.setIs24HourView(true);
                                      builder.setTitle("Please select time at which you would be leaving :")
                                              .setView(picker)
                                              .setPositiveButton("OK",
                                                      new DialogInterface.OnClickListener() {
                                                          public void onClick(DialogInterface dialog, int whichButton) {
                                                              int hour = picker.getCurrentHour();
                                                              int minute = picker.getCurrentMinute();
                                                              leaving.setTextSize(14);
  
                                                              leaving.setText(s + " " + hour + ":" + minute);
                                                              inTime=s2+" "+hour+":"+minute;
                                                              Log.e("inTime ",inTime);
                                                          }
                                                      })
                                              .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                                                  public void onClick(DialogInterface dialog, int whichButton) {
                                                      leaving.setText("");
                                                      inTime="";
                                                      dialog.dismiss();
                                                  }
                                              }).create().show();
                                      //Time picker
                                  }
                              }
                      )
                      .setNegativeButton("Cancel",
                              new DialogInterface.OnClickListener() {
                                  public void onClick(DialogInterface dialog, int whichButton) {
                                      leaving.setText("");
                                      inTime="";
                                      dialog.dismiss();
                                  }
                              }
                      )
                      .create().show();
  </code></pre>
  
  <p>Please help. Thank you</p>