2

I have created a datepicker and set the max date to the current date but I am getting a weird bug. It shows the next month and next day below the wheel when nothing should be there. When you try to scroll to them they disappear and are not selectable.

my datepicker code:

    DatePickerDialog dialog = new DatePickerDialog(this, datePickerListener, year, month, day);
    dialog.getDatePicker().setMaxDate(new Date().getTime());
    return dialog;

how im getting year, month, day:

    final Calendar c = Calendar.getInstance();
    year = c.get(Calendar.YEAR);
    month = c.get(Calendar.MONTH);
    day = c.get(Calendar.DAY_OF_MONTH);

Here is a picture of what is happening

enter image description here

BluGeni
  • 3,378
  • 8
  • 36
  • 64

1 Answers1

1

I had the same problem. My solution was to set also the hour of the Calendar object to 11:

yourCalendarObject.set(Calendar.HOUR, 11);

And then use the calendar object to set the setMaxDate from your Datepicker.

yourDatePicker.setMaxDate(yourCalendarObject.getTimeInMillis());

Hope it helps anyone.

EDIT: I highly recommend to use a library for all this. It takes care of the complete handling with the datpickers and also solved my bug: https://github.com/wdullaer/MaterialDateTimePicker

Tristan
  • 11
  • 2
  • You would still get in trouble with the minutes if you only set the hours and it is somewhere withing 11 AM or PM. Because as soon as you then set the maxdate you have already exceeded that max date because it a timestamp. I explicitly set the maxdate to 23:59:59 PM (AM_PM = 1). – Ferry de Boer Dec 21 '15 at 10:14