1

I'm using datepicker, but whenever I'm selecting any date its showing -1 moneth. For example- If I select 12/12/2016, it'll display in textbox 12/11/2016 If I select 3/1/2017, it'll display 3/0/2017

Here is my piece of code of datepicker dialog:

editStartDate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Calendar mcurrentDate = Calendar.getInstance();
                    int mYear = mcurrentDate.get(Calendar.YEAR);
                    int mMonth = mcurrentDate.get(Calendar.MONTH);
                    int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);

                    DatePickerDialog mDatePicker = new DatePickerDialog(TourActivity.this, new DatePickerDialog.OnDateSetListener() {
                        public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
                            Calendar newDate = Calendar.getInstance();
                            newDate.set(selectedyear, selectedmonth, selectedday);
                            editStartDate.setText(selectedday + "/" + selectedmonth + "/" + selectedyear);
                        }
                    }, mYear, mMonth, mDay);
                    mDatePicker.setTitle("Select date");
                    mDatePicker.show();

                }
            });

Please suggest where I'm putting wrong code.

Kiran Malvi
  • 636
  • 2
  • 9
  • 29

1 Answers1

6

In this case, the month count is started at 0. So you have to add 1 for display.

  editStartDate.setText(selectedday + "/" + (selectedmonth + 1) + "/" + selectedyear);

See also this question for reference: Why is January month 0 in Java Calendar?

Community
  • 1
  • 1
NineBerry
  • 26,306
  • 3
  • 62
  • 93