Pritom Mazumdar
Pritom Mazumdar

Reputation: 335

DatePicker showing month value starting from 0

I am using two datepickers one for start date and one for end date in one activity and whenever I select a month,it shows the wrong number. I tried adding +1 to the month value but since there are two datepickers and we have to skip the days before the start date for selection so it is not working. The code is given below :

public static class DatePickerFragment extends DialogFragment implements
        DatePickerDialog.OnDateSetListener {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);
        DatePickerDialog datePickerDialog;
        datePickerDialog = new DatePickerDialog(getActivity(), this, year,
                month, day);
        return datePickerDialog;
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        // Do something with the date chosen by the user

        cval_from.setText(day + "/" + month + "/" + year);
    }
}

public static class ToDatePickerFragment extends DialogFragment implements
        DatePickerDialog.OnDateSetListener {
    // Calendar startDateCalendar=Calendar.getInstance();
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        String getfromdate = cval_from.getText().toString().trim();
        String getfrom[] = getfromdate.split("/");
        int year, month, day;
        year = Integer.parseInt(getfrom[2]);
        month = Integer.parseInt(getfrom[1]);
        day = Integer.parseInt(getfrom[0]);
        final Calendar c = Calendar.getInstance();
        c.set(year, month, day + 1);
        DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), this, year, month, day);
        datePickerDialog.getDatePicker().setMinDate(c.getTimeInMillis());
        return datePickerDialog;
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {

        cval_to.setText(day + "/" + month + "/" + year);
    }

}

Here,cval_from is the start date and cval_to is the end date,now if I add month+1 in cval_from the cval_to selection of dates starts from the next month. For example if I select 26/1/2018 for cval_from,I have to select cval_to from 27/1/2018 onwards. The whole month is skipped.

Can anyone help?

Upvotes: 1

Views: 2369

Answers (2)

Farhana Naaz Ansari
Farhana Naaz Ansari

Reputation: 7944

You can use two date picker at different click when you use for start date send date in method which you want to set initially in the DatePicker as well as end date and call this method at Click

void selectDate(int mDay ,int mMonth,int mYear) {

    DatePickerDialog datePickerDialog = new DatePickerDialog(this,R.style.mydatetheame,
            new DatePickerDialog.OnDateSetListener() {

                @Override
                public void onDateSet(DatePicker view, int year,
                                      int monthOfYear, int dayOfMonth) {

                    tv_date.setText((monthOfYear + 1) + "/" + dayOfMonth + "/" + year);

                }
            }, mYear, mMonth, mDay);
    datePickerDialog.show();

}

and of course, you will get a correct month by this method.

Upvotes: 2

Zeero0
Zeero0

Reputation: 2780

According to developer.android.com month ranges from 0-11.

So you need to increment it by 1.

public void onDateSet(DatePicker view, int year, int month, int day) {
    int selectedMonth = month+1;
    cval_to.setText(day + "/" + selectedMonth + "/" + year);
}

Upvotes: 5

Related Questions