Skip to content
Advertisement

Android DatePicker dialog, date should come from tomorrow

 Calendar calender = Calendar.getInstance();
    final CustomDatePickerDialog pickerDialog = new CustomDatePickerDialog(LabCheckOutActivity.this,
    myDateListener, calender.get(Calendar.YEAR), calender.get(Calendar.MONTH),
    calender.get(Calendar.DAY_OF_MONTH)+1);
    pickerDialog.getDatePicker().setMinDate(System.currentTimeMillis()-1000);
    pickerDialog.show();

by Using this code, in dialog date is pointed to tomorrow but user can also select todays date.I want user can select date from tomorrow not today.

public class CustomDatePickerDialog extends DatePickerDialog {

int maxYear;
int maxMonth;
int maxDay;

public CustomDatePickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) {
    super(context, callBack, year, monthOfYear, dayOfMonth);
}

public void setMaxDate(long maxDate) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        getDatePicker().setMaxDate(System.currentTimeMillis());
    } else {
        final Calendar c = Calendar.getInstance();
        c.setTimeInMillis(maxDate);
        maxYear = c.get(Calendar.YEAR);
        maxMonth = c.get(Calendar.MONTH);
        maxDay = c.get(Calendar.DAY_OF_MONTH);
    }
}

@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        super.onDateChanged(view, year, monthOfYear, dayOfMonth);
    } else {
        if (year > maxYear)
            view.updateDate(maxYear, maxMonth, maxDay);

        if (monthOfYear > maxMonth && year == maxYear)
            view.updateDate(maxYear, maxMonth, maxDay);

        if (dayOfMonth > maxDay && year == maxYear && monthOfYear == maxMonth)
            view.updateDate(maxYear, maxMonth, maxDay);
    }
}

}

Advertisement

Answer

Use

pickerDialog.getDatePicker().setMinDate(System.currentTimeMillis()+24*60*60*1000);//where 24*60*60*1000 represents the total timestamp for one day

instead of

  pickerDialog.getDatePicker().setMinDate(System.currentTimeMillis()-1000);
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement