选择日期后,其显示错误的月份

时间:2017-01-15 05:46:33

标签: android datepicker

我使用了datepicker,但每当我选择显示-1 moneth的任何日期时。 例如 - 如果我选择12/12/2016,它将在文本框12/11/2016中显示 如果我选择3/1/2017,它将显示3/0/2017

这是我的datepicker对话框代码:

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();

                }
            });

请说明我在哪里放错了代码。

1 个答案:

答案 0 :(得分:3)

在这种情况下,月份计数从0开始。因此您必须添加1才能显示。

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

另请参阅此问题以供参考:Why is January month 0 in Java Calendar?