2017-01-15 41 views
1

我使用datepicker,但每當我選擇任何日期它顯示-1 moneth。 例如,如果我選擇12/12/2016,它將顯示在文本框中12/11/2016 如果我選擇3/1/2017,它將顯示2017/3/2017選擇日期後,它顯示錯誤的月份

這是我的日期選擇器對話框的代碼片段:

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

       } 
      }); 

請建議我在哪裏輸入錯誤代碼。

回答

2

在這種情況下,月份數從0開始。因此,您必須添加1才能顯示。

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

又見這個問題以供參考:Why is January month 0 in Java Calendar?

相關問題