Search code examples
androidandroid-studiodatepickerandroid-datepickerdatepickerdialog

Date Picker - Change Selected Date Android


I have used Date Picker and applied max date as 11th Feb, 2020 when today's date is 12th Feb, 2019.

I am having an issue while changing year directly on clicking 'year' on top of date picker. When date picker is initialized on current date (today's date: 12th Feb, 2019), then if I change year to Feb 2020, date picker shows 12th Feb, 2020 as title but selected day on calendar view is 11th Feb as selected.

See Below images::

img 1 : on date picker initialized as today's date

enter image description here

img 2 : without changing day, change year

enter image description here

img 3 : title is 12th feb, 2020 as selected. But as max date is 11th Feb, 2020. 11th is selected on calendar. Also, on clicking Ok, 12th feb, 2020 is obtained.

enter image description here

I want to remove this selection or just make max date as selected if year changes.


Solution

  • After several hit and trials, and several google searches, finally found a solution to above:

            datePicker.init(currentCalendar.get(Calendar.YEAR),
            currentCalendar.get(Calendar.MONTH),
            currentCalendar.get(Calendar.DAY_OF_MONTH),
            new DatePicker.OnDateChangedListener() {
              @Override
              public void onDateChanged(DatePicker view, int selYear, int selMonth,
                  int selDay) {
                String dateSelectionType =
                    getDateSelectionType(minDate.getTimeInMillis(),
                        maxDate.getTimeInMillis(), selYear, selMonth, selDay);
                if (dateSelectionType.equals("IS_BETWEEN_RANGE")) {
                  return;
                }
                if (dateSelectionType.equals("IS_LESS_THAN_MIN")) {
                  datePicker.updateDate(minDate.get(Calendar.YEAR), minDate.get(Calendar.MONTH),
                      minDate.get(Calendar.DAY_OF_MONTH));
    
                  return;
                }
                if (dateSelectionType.equals("IS_MORE_THAN_MAX")) {
                  datePicker.updateDate(maxDate.get(Calendar.YEAR),
                      maxDate.get(Calendar.MONTH), maxDate.get(Calendar.DAY_OF_MONTH));
                  return;
                }
              }
            });
    

    add another method to know if date selected is after max date or before min date ::

    public static String getDateSelectionType(long minDate, long maxDate,
              int selectedYear, int selectedMonth, int selectedDayOfMonth) {
    
    
    Calendar minDateCalendar = Calendar.getInstance();
        minDateCalendar.setTimeInMillis(minDate);
        minDateCalendar.set(Calendar.HOUR_OF_DAY, 0);
        minDateCalendar.set(Calendar.MINUTE, 0);
        minDateCalendar.set(Calendar.SECOND, 0);
        minDateCalendar.set(Calendar.MILLISECOND, 0);
    
        Calendar maxDateCalendar = Calendar.getInstance();
        maxDateCalendar.setTimeInMillis(maxDate);
        maxDateCalendar.set(Calendar.HOUR_OF_DAY, 0);
        maxDateCalendar.set(Calendar.MINUTE, 0);
        maxDateCalendar.set(Calendar.SECOND, 0);
        maxDateCalendar.set(Calendar.MILLISECOND, 0);
    
        Calendar selectedDateCalendar = Calendar.getInstance();
        selectedDateCalendar.set(Calendar.YEAR, selectedYear);
        selectedDateCalendar.set(Calendar.MONTH, selectedMonth);
        selectedDateCalendar.set(Calendar.DAY_OF_MONTH, selectedDayOfMonth);
        selectedDateCalendar.set(Calendar.HOUR_OF_DAY, 0);
        selectedDateCalendar.set(Calendar.MINUTE, 0);
        selectedDateCalendar.set(Calendar.SECOND, 0);
        selectedDateCalendar.set(Calendar.MILLISECOND, 0);
    
        if (selectedDateCalendar.getTime().before(minDateCalendar.getTime()))
          return "IS_LESS_THAN_MIN";
    
        if (selectedDateCalendar.getTime().after(maxDateCalendar.getTime()))
          return "IS_MORE_THAN_MAX";
    
        return "IS_BETWEEN_RANGE";
      }
    

    PS: Adding the solution to my own question is just a way out if anyone faces the same issue.