4

请参考下图:

在此处输入图像描述

上面的文字说 Thu, Oct 28, 1909...而 datepicker 实际上的值是 3rd jan, 1902。错误是什么?

这是我的代码:

@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() 方法。

我究竟做错了什么?

4

2 回答 2

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

这将以正确的格式显示日期

于 2013-07-17T09:31:36.480 回答
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();
于 2013-07-17T08:58:00.177 回答