Mark
Mark

Reputation: 39

How to let the user pick the future date with datepickerdialog?

I want to let the user pick the date after today, as a remind date, how can I do it?Also, how can I put the theme inside these codes,because there is no button for the user to choose.

Here is my code combine with datapickerdialog and timepickerdialog:

private void setDateTimeField (){

    DatePickerDialog datePickerDialog = new DatePickerDialog(this, new 
DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker datePicker, int selectedYear, int selectedMonth, int selectedDate) {

            year = selectedYear;
            month = selectedMonth;
            day = selectedDate;

            timePicker();
            date_time = year + "-" + (month + 1) + "-" + day;
        }
    },year, month, day);

    final Calendar calendar = Calendar.getInstance();
    Calendar cal = Calendar.getInstance();
    int yy = calendar.get(Calendar.YEAR);
    int mm = calendar.get(Calendar.MONTH);
    int dd = calendar.get(Calendar.DAY_OF_MONTH);

    cal.set(Calendar.MONTH, mm);
    cal.set(Calendar.DAY_OF_MONTH, dd);
    cal.set(Calendar.YEAR, yy);

    datePickerDialog.getDatePicker().setMinDate(cal.getTimeInMillis());

    datePickerDialog.show();
  }
private void timePicker(){
    // Get Current Time
    final Calendar c = Calendar.getInstance();
    hours = c.get(Calendar.HOUR_OF_DAY);
    minutes = c.get(Calendar.MINUTE);
    // Launch Time Picker Dialog
     TimePickerDialog timePickerDialog = new TimePickerDialog(this,new 
TimePickerDialog.OnTimeSetListener() {

                @Override
                public void onTimeSet(TimePicker view, int hourOfDay, int 
minute) {
                    hours = hourOfDay;
                    minutes = minute;
                    modifytime.setText(date_time+" "+hourOfDay + ":" + 
minute);
                }

            }, hours, minutes, false);
    timePickerDialog.show();
}

Upvotes: 0

Views: 113

Answers (1)

Bethan
Bethan

Reputation: 971

If you like to set the current date as minimum date and no past days should be selectable, then below solution will works I believe

Calendar cal = Calendar.getInstance();
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
//Min date setting part
cal.set(Calendar.MONTH, mm);
cal.set(Calendar.DAY_OF_MONTH, dd);
cal.set(Calendar.YEAR, yy);
//Replace your code mDatePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis()); with the below line
mDatePickerDialog.setMinDate(cal.getTimeInMillis());

Since you are allowing the user to select only future dates, you should not need to set Max date.

Complete Code sample for your reference -

private void showDateDailog() {

    final DatePickerDialog datePickerDialog = new DatePickerDialog(getApplicationContext(), new DatePickerDialog.OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker datePicker, int selectedYear, int selectedMonth, int selectedDate) {

        year = selectedYear;
        month = selectedMonth;
        day = selectedDate;

        addtime.setText(new StringBuilder().append(year).append("/")
                .append(month + 1).append("/").append(day));

        }
    }, year, month, day);

    final Calendar calendar = Calendar.getInstance();
    Calendar cal = Calendar.getInstance();
    int yy = calendar.get(Calendar.YEAR);
    int mm = calendar.get(Calendar.MONTH);
    int dd = calendar.get(Calendar.DAY_OF_MONTH);
    //Min date setting part
    cal.set(Calendar.MONTH, mm);
    cal.set(Calendar.DAY_OF_MONTH, dd);
    cal.set(Calendar.YEAR, yy);
    datePickerDialog.setMinDate(cal.getTimeInMillis());
    //Maximum date setting part, if you need (Else don't add it)
    /*Calendar calen = Calendar.getInstance();
    calen.set(Calendar.MONTH, mm);
    calen.set(Calendar.DAY_OF_MONTH, dd);
    calen.set(Calendar.YEAR, yy + 2);
    datePickerDialog.setMaxDate(calen.getTimeInMillis());*/
    datePickerDialog.show();
}

Upvotes: 1

Related Questions