0

我目前的问题是从 DatePickerDialog 获得的值在我将它们发送到 Date 构造函数时没有得到正确解析。

获取日期值(根据另一篇文章的建议)....

private DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        mYear = year;
        mMonth = monthOfYear + 1;
        mDayOfMonth = dayOfMonth;
        String t = "listener => mYear=" + mYear + "; mMonth=" + mMonth +
                "; mDayOfMonth=" + mDayOfMonth + ";";
        Log.i(TAG, t);
    }
};

上面的日志语句产生:

04-30 21:34:10.548: I/TodosActivity(29510): listener => mYear=2012; mMonth=5; mDayOfMonth=2;

日期是用......

todo.setDueDate(new Date(mYear, mMonth, mDayOfMonth));

我验证了到期日......

String t = "setNewDueDate(Todo) => #" + todo.getId() + ": " + todo.getTitle() + " (" +
            todo.getText() + "). Created on " + format.format(todo.getCreatedDate()) +
            ". Last modified on " + format.format(todo.getModifiedDate()) +
            ". Due on " + format.format(todo.getDueDate()) + ".";
Log.i(TAG, t);

这导致....

04-30 21:34:05.703: I/TodosActivity(29510): setNewDueDate(Todo) => #1: Test (Test). Created on 30 Apr 2012 20:07:51. Last modified on 30 Apr 2012 20:55:27. Due on 31 Dec 1899 00:00:00.

编辑:

设置初始 DatePickerDialog 值...

        return new DatePickerDialog(this, listener, cal.get(Calendar.YEAR),
                cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));

有什么我应该/不应该做的事吗?

4

1 回答 1

4

使用Calendar和它上面的各种set()方法来更新日期。

例如,这个示例项目的代码如下:

 DatePickerDialog.OnDateSetListener d=new DatePickerDialog.OnDateSetListener() {
    public void onDateSet(DatePicker view, int year, int monthOfYear,
                          int dayOfMonth) {
      dateAndTime.set(Calendar.YEAR, year);
      dateAndTime.set(Calendar.MONTH, monthOfYear);
      dateAndTime.set(Calendar.DAY_OF_MONTH, dayOfMonth);
      updateLabel();
    }
  };

哪里dateAndTimeCalendar

于 2012-04-30T12:55:46.503 回答