2

I am displaying a datepicker using the datepicker fragment. I init my datepicker with my previously selected date.

While loading datepicker with the previous date I am unable to select the current date from datepicker. I face this issue in API Level 21 only. It's working in all API levels expects API 21.

Whenever I remove maxDate() from datepicker, It started working out. But in my case, I need to disable future dates.

I don't understand what is wrong with my code.

Here is my code.

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

    public DatePickerFragment() {
    }

    private String selectedDate = "";
    private Interfaces.OnDateSelectedClickListener onDateSelectedClickListener;

    public DatePickerFragment(Interfaces.OnDateSelectedClickListener onDateSelectedClickListener, String selectedDate) {
        this.onDateSelectedClickListener = onDateSelectedClickListener;
        this.selectedDate = selectedDate;

    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Calendar calendar = Calendar.getInstance();
        int year = 0, month = 0, day = 0;

        if (StaticUtils.isTextEmpty(selectedDate)) {
            year = calendar.get(Calendar.YEAR);
            month = calendar.get(Calendar.MONTH);
            day = calendar.get(Calendar.DAY_OF_MONTH);
        } else {
            try {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy", Locale.getDefault());
                Date date = simpleDateFormat.parse(selectedDate);
                calendar.setTimeInMillis(date.getTime());
                year = calendar.get(Calendar.YEAR);
                month = calendar.get(Calendar.MONTH);
                day = calendar.get(Calendar.DAY_OF_MONTH);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        DatePickerDialog datePicker;
        if (Build.VERSION.SDK_INT < 21) {
            datePicker = new DatePickerDialog(getActivity(), AlertDialog.THEME_HOLO_LIGHT, this, year, month, day);
        } else {
            datePicker = new DatePickerDialog(getActivity(), R.style.datepicker, this, year, month, day);
        }

        datePicker.getDatePicker().setMaxDate(System.currentTimeMillis()); // disable future dates
        datePicker.setTitle("");
        datePicker.setCustomTitle(null);
        return datePicker;
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        if (onDateSelectedClickListener != null) {
            onDateSelectedClickListener.onDateSelect(StaticUtils.getDateTimeFromCalendar(day, month, year));
        }
    }
}

Update :

After applying @Lalchand code I am able to select the current date if I have loaded my calendar with the previous date but still, in API Level 21 it is not working. Can anyone help me? Or give me the suggestion to improve my code.

Riddhi Shah
  • 477
  • 7
  • 26

4 Answers4

3

After few more research, I come to know that system will not allow me to select my current date as I have set the maximum date. So to allow my user to select current date I change

datePicker.getDatePicker().setMaxDate(System.currentTimeMillis()); to

datePicker.getDatePicker().setMaxDate(System.currentTimeMillis() + (1000 * 60 * 60));

By applying above work around it allow me to select current date and as well as load my calendar with previous date.

Riddhi Shah
  • 477
  • 7
  • 26
2

Following code block will help you.

Pass selectedDate = "03/04/2017".

private void datePickerDialog(String selectedDate) {
        Calendar calendar = Calendar.getInstance();
        if (StaticUtils.isTextEmpty(selectedDate)) {
            calendar = Calendar.getInstance();
        } else {
            try {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy", Locale.getDefault());
                Date date = simpleDateFormat.parse(selectedDate);
                calendar.setTimeInMillis(date.getTime());
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        DatePickerDialog datePickerDialog = new DatePickerDialog(this,
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker arg0, int year, int month, int day) {
                        try {
                            StringBuilder calDate = new StringBuilder().append(day).append("/").append(month + 1).append("/").append(year);
                            Log.d("TAG", "Current selected date : " + calDate.toString());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                },
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH));

        datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis());
        datePickerDialog.show();
    }
Lalchand
  • 105
  • 7
0

I think below link will provide you answer:

Able to choose date even after setMaxDate

Therefore, after user select date, give warn or alert message to user if they have selected wrong date.

Solution Found: Use library as per mention in this link

setMaxDate() not working perfectly in lollipop

Community
  • 1
  • 1
Nitin Patel
  • 1,605
  • 13
  • 31
-1
datePicker.getDatePicker().setMaxDate((maxDate + 1).getTime() - 1)

I guess that they write the code like below:

if (getCurrentTimeInMillis() > getMaxDate().getTimeInMillis()) {
    // you can't select the future time
    return;
}

After bug fix, it should be like below:

if (getCurrentDate() > getMaxDate()) {
    // you can't select the future time
    return;
}

It is obvious that you can fix it by set to the max timeInmillis in the day

Ping Jia
  • 19
  • 3
  • While this code may solve the question, [including an explanation](https://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanation, and give an indication of what limitations and assumptions apply. – double-beep Mar 01 '19 at 18:03