0

I have 2 DatePickers, one is start date picker and the other is end date picker. I want to set the min date of the end DatePicker to the start date which user has selected.

I set the Data which user selected in Calander startDate and set this data as MinDate at end date. I've tried in this way but it's not working :(

I can't use Mateiral Range Date Picker because I have to use specific theme. Also I'm not good at programming so I'd appreciate it if you could explain it in detail.

ipDcEventStartDay.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v){
        final Calendar c = Calendar.getInstance();

        startYear = c.get(Calendar.YEAR);
        startMonth = c.get(Calendar.MONTH);
        startDay = c.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog dpd = new DatePickerDialog(DoubleCheckEventActivity.this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                startYear = year;
                startMonth = month+1;
                startDay = dayOfMonth;

                ipDcEventStartDay.setText(startYear + " - " + startMonth + " - " + startDay);
                ipDcEventStartDay.setTextColor(Color.parseColor("#000000"));
            }
        }, startYear, startMonth, startDay);
        dpd.show();
    }
});

Calendar startDate = Calendar.getInstance();
startDate.set(Calendar.YEAR, startYear);
startDate.set(Calendar.MONTH, startMonth);
startDate.set(Calendar.DAY_OF_MONTH, startDay);

ipDcEventEndDay.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        final Calendar c = Calendar.getInstance();

        endYear = c.get(Calendar.YEAR);
        endMonth = c.get(Calendar.MONTH);
        endDay = c.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog dpd = new DatePickerDialog(DoubleCheckEventActivity.this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                endYear = year;
                endMonth = month;
                endDay = dayOfMonth;

                ipDcEventEndDay.setText(endYear + " - " + (endMonth+1) + " - " + endDay);
                ipDcEventEndDay.setTextColor(Color.parseColor("#000000"));
            }
        }, endYear,endMonth, endDay);
        dpd.getDatePicker().setMinDate(startDate.getTimeInMillis());
        dpd.show();
    }
});
  • Can you post your implementation of min and max date? Maybe there is some problem there, because those are the methods used. I suposse you already check [this post](https://stackoverflow.com/questions/16749361/how-set-maximum-date-in-datepicker-dialog-in-android) – javdromero Apr 13 '21 at 16:23
  • @javdromero Those codes are all I did.. I set the end date Date Picker to `dpd.getDatePicker().setMinDate(startDate.getTimeInMillis()); dpd.show();` – Soyoung Kim Apr 13 '21 at 16:35
  • Don’t post [the same question](https://stackoverflow.com/questions/67074925/how-can-i-set-the-specific-selected-date-to-min-date-in-android-date-picker) again. Instead edit your previous question to improve it. – Ole V.V. Apr 14 '21 at 03:32

1 Answers1

0

This code:

startDate.set(Calendar.YEAR, startYear);
startDate.set(Calendar.MONTH, startMonth);
startDate.set(Calendar.DAY_OF_MONTH, startDay);

Should be inside

ipDcEventEndDay.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
}

Because startYear, startMonth and startDay are only set on ipDcEventStartDay.setOnClickListener(), so at the beginning they have some value but not the one that was set on ipDcEventStartDay.setOnClickListener()

So in ipDcEventEndDay.setOnClickListener() you have to set again on startDate the values that were picked on the other DatePickerDialog

javdromero
  • 1,850
  • 2
  • 11
  • 19
  • Thanks a lot! I understand :) Can I ask you one more question? I want to convert that startDate and endDate Calendar value to string. I add this code just after that ipDcEventDay.setOnClickLister { ... } but both startDate and endDate logged 00021231 – Soyoung Kim Apr 13 '21 at 18:03
  • This is my code what I tried `Calendar startDate = Calendar.getInstance(); startDate.set(startYear, startMonth, startDay); Date start = startDate.getTime(); Calendar endDate = Calendar.getInstance(); endDate.clear(); endDate.set(endYear,endMonth,endDay); Date end = endDate.getTime(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd"); String startDay = simpleDateFormat.format(start); String endDay = simpleDateFormat.format(end); Log.d("start",startDay); Log.d("end",endDay);` – Soyoung Kim Apr 13 '21 at 18:06
  • Please put that code on the question as an edit, it's not understandable in a comment, also you can simple convert using `startDate.getTime().toString()` – javdromero Apr 13 '21 at 18:15