2014-04-14 65 views
0

我有這個日期選擇器。這個Android日期選擇器有什麼問題?

例如,如果我選擇2011年12月11日顯示==> 2011:11:11

或2012年6月11日顯示==> 2012:0:11 !!!

private void Date_picker_dialog() 
{ 

    OnDateSetListener datelistener = new OnDateSetListener() { 

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

      Toast.makeText(ActivityMain.this, "Date set: " + year + ":" + monthOfYear + ":" + dayOfMonth, 3000).show(); 

     } 
    }; 
    DatePickerDialog dialog = new DatePickerDialog(ActivityMain.this, datelistener, 2011, 11, 11); 
    dialog.show(); 

} 
+0

你必須使用(monthOfYear + 1)becz默認情況下它與0 .... –

+0

月從0開始列舉所以當你從datepicker得到你的日期,月份設置爲12月時,你會得到monthOfYear = 11 –

回答

0
// try this way hope this will help you.... 
    OnDateSetListener datelistener = new OnDateSetListener() { 

    @Override 
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 
     Toast.makeText(ActivityMain.this, "Date set: " + year + ":" + (monthOfYear+1) + ":" + dayOfMonth, 3000).show(); 
    } 
    }; 
    DatePickerDialog dialog = new DatePickerDialog(ActivityMain.this, datelistener, 2011, 11, 11); 
    dialog.show(); 
0
private void callDatePic() { 
    new DatePickerDialog(AddFriendActivity.this, d, c.get(Calendar.YEAR), 
      c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH)).show(); 

} 

DatePickerDialog.OnDateSetListener d = new OnDateSetListener() { 

    @Override 
    public void onDateSet(DatePicker view, int year, int monthOfYear, 
      int dayOfMonth) { 
     date = dayOfMonth + "-" + (monthOfYear + 1) + "-" + year;   
     c1.set(Calendar.MONTH, monthOfYear); 
     c1.set(Calendar.YEAR, year); 
     c1.set(Calendar.DAY_OF_MONTH, dayOfMonth); 
     Toast.makeText(ActivityMain.this, "Date set: " + date,3000).show(); 

    } 
    } 

}; 

試試這個 希望這將有助於你