我想從datepicker中獲取所選日期,但無法獲得正確的值。我的代碼如下。Android的DatePicker值顯示錯誤的值
View mView = getLayoutInflater().inflate(R.layout.set_date_layout, null);
dialog = new MaterialDialog.Builder(RemainderActivity.this);
dialog.title(R.string.set_date);
datePicker = (DatePicker) mView.findViewById(R.id.datePickerRemainder);
dialog.customView(mView, true);
dialog.positiveText(R.string.confirm_date);
dialog.negativeText(android.R.string.cancel);
dialog.cancelable(false);
dialog.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
Log.d("YEAR", datePicker.getYear()+""); //Shows correct year as 2017
Date date = new Date(datePicker.getDayOfMonth(), (datePicker.getMonth() + 1),datePicker.getYear()); // Wrong date as 09-04-1907
String dateString = df.format(date);
txt_remainder_date_add.setText(dateString);
}
});
dialog.build();
dialog.show();
你想什麼日期選擇和你包括兩個! – phpdroid
我想選擇2017年9月2日它顯示08年8月8日..如果我選擇01年9月01日它顯示04年9月1907 –
因爲在日期選擇器月從0開始而不是從1開始,請參閱我的代碼如下,並嘗試使用它,希望它適用於你 –