Datepicker and timepicker - set max and min values

try this:

btnDate.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        final DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

            }
        };

        Calendar c = Calendar.getInstance();
        int mYear = c.get(Calendar.YEAR);
        int mMonth = c.get(Calendar.MONTH);
        int mDay = c.get(Calendar.DAY_OF_MONTH);

        final DatePickerDialog datePickerDialog = new DatePickerDialog(
                Main23Activity.this, datePickerListener,
                mYear, mMonth, mDay);
        DatePicker datePicker = datePickerDialog.getDatePicker();

        c.add(Calendar.MONTH, +1);
        long oneMonthAhead = c.getTimeInMillis();
        datePicker.setMaxDate(oneMonthAhead);
        datePicker.setMinDate(System.currentTimeMillis() - 1000);
        datePickerDialog.show();

    }
});

btnTime.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hour, int minute) {

            }
        };
        Calendar c = Calendar.getInstance();

        final TimePickerDialog timePickerDialog = new TimePickerDialog(Main23Activity.this,timePickerListener,
                c.get(Calendar.HOUR_OF_DAY),c.get(Calendar.MINUTE)+5,false);
        timePickerDialog.show();

    }
});

You need to check if the user selects today date and the time is less than the current time condition though.


For DatePicker try this

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.YEAR, cal.MONTH, cal.DAY_OF_MONTH);
            dialog.getDatePicker().setMinDate(System.currentTimeMillis());
            cal1.add(Calendar.YEAR, 5);
            dialog.getDatePicker().setMaxDate(cal1.getTimeInMillis());
            dialog.show();
        }
    });
  }
}

For TimePicker try this example


I combined the date and time pickers and added a condition to timePicker.

    public class TestActivity extends Activity {

        private Button mPickDate;
        private TextView mDateDisplay;
        private TextView mTimeDisplay;

        final Calendar c = Calendar.getInstance();
        private int mYear = c.get(Calendar.YEAR);
        private int mMonth = c.get(Calendar.MONTH);
        private int mDay = c.get(Calendar.DAY_OF_MONTH);
        private int mHour = c.get(Calendar.HOUR_OF_DAY);
        private int mMinute = c.get(Calendar.MINUTE);

        static final int TIME_DIALOG_ID = 1;
        static final int DATE_DIALOG_ID = 0;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_test);
            });

            mDateDisplay = (TextView) findViewById(R.id.dateTextView);
            mPickDate = (Button) findViewById(R.id.datepicker);
            mTimeDisplay = (TextView) findViewById(R.id.timeTextView);

            mPickDate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);
        }
    });

    });

    mPickDate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);
        }
    });

}

// Update date and time
private void updateDate() {
    mDateDisplay.setText(
            new StringBuilder()
                    .append(mDay).append("/")
                    .append(mMonth + 1).append("/")
                    .append(mYear).append(" "));
    showDialog(TIME_DIALOG_ID);
}

public void updateTime() {
    mTimeDisplay.setText(
            new StringBuilder()
                    .append(pad(mHour)).append(":")
                    .append(pad(mMinute)));
}

private static String pad(int c) {
    if (c >= 10)
        return String.valueOf(c);
    else
        return "0" + String.valueOf(c);
}

// Generate DatePickerDialog and TimePickerDialog
private DatePickerDialog.OnDateSetListener mDateSetListener =
        new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int year,
                                  int monthOfYear, int dayOfMonth) {
                mYear = year;
                mMonth = monthOfYear;
                mDay = dayOfMonth;
                updateDate();
            }
        };

private TimePickerDialog.OnTimeSetListener mTimeSetListener =
        new TimePickerDialog.OnTimeSetListener() {
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                mHour = hourOfDay;
                mMinute = minute;

                Calendar c2 = Calendar.getInstance();

                if (mYear == c2.get(Calendar.YEAR)
                        && mMonth == c2.get(Calendar.MONTH)
                        && mDay == c2.get(Calendar.DAY_OF_MONTH)
                        && (mHour < c2.get(Calendar.HOUR_OF_DAY) || (mHour == c2.get(Calendar.HOUR_OF_DAY) && mMinute <= (c2.get(Calendar.MINUTE)+10))
                    )
                ) {
                    Toast.makeText(TestActivity.this, "Set time at least 10 minutes from now", Toast.LENGTH_LONG).show();
                } else {
                    updateTime();
                }

            }
        };

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
        case DATE_DIALOG_ID:
            DatePickerDialog datePickerDialog = new DatePickerDialog(this,
                    mDateSetListener,
                    mYear, mMonth, mDay);
            c.add(Calendar.MONTH, +1);
            long oneMonthAhead = c.getTimeInMillis();
            datePickerDialog.getDatePicker().setMaxDate(oneMonthAhead);
            datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
            return datePickerDialog;

        case TIME_DIALOG_ID:
            TimePickerDialog timePickerDialog =
                    new TimePickerDialog(this,
                            mTimeSetListener, mHour, mMinute, false);

            return timePickerDialog;
    }
    return null;}
}