选择日期后,显示错误的月份 [英] After selecting date, its showing wrong month

查看:75
本文介绍了选择日期后,显示错误的月份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用日期选择器,但是无论何时选择显示-1个月份的任何日期。
例如-如果我选择12/12/2016,它将显示在文本框中12/11/2016
如果我选择3/1/2017,它将显示3/0/2017

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.

推荐答案

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

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

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

另请参阅以下问题以供参考:为什么Java日历中的1月为0?

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

这篇关于选择日期后,显示错误的月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆