请参考下图:
上面的文字说1909年10月28日星期四...而日期拣货者的价值实际上是1902年3月1日。 这是什么错误?
这是我的代码:
@SuppressWarnings("deprecation")
public void setTheDate(View v){
showDialog(DATE_DIALOG_ID);
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
// set date picker as current date
return new DatePickerDialog(this, datePickerListener,
year, month,day);
}
return null;
}
private DatePickerDialog.OnDateSetListener datePickerListener
= new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
System.out.println("date" + year + month + day);
}
};
DATE_DIALOG_ID是一个最终常量,我点击一个按钮就调用了setTheDate()方法。
我做错了什么?
答案 0 :(得分:1)
您可以使用此代码从日期选择器中检索值。
在onDateSet() method of OnDateSetListener()
TimestampField field = (TimestampField) fieldpicker;
Timestamp timestamp = field.getTimestamp();
timestamp.setYear(year - 1900);
timestamp.setMonth(monthOfYear);
timestamp.setDate(dayOfMonth);
field.setTimestamp(timestamp);
这将以正确的格式显示日期
答案 1 :(得分:0)
只需尝试一下,类似于您的代码,但它对我有用。
Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
y = c.get(Calendar.YEAR);
m = c.get(Calendar.MONTH);
dy = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
Log.d("err", "onselect");
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
}
};
DatePickerDialog d = new DatePickerDialog(getActivity(),
mDateSetListener, mYear, mMonth, mDay);
d.show();