1

我在我的 android 应用程序中遇到了一些奇怪的问题,当我在 datepicker 中为 ex 选择日期时,15march2013 我得到 9october2007

这是来自魔法的片段

public static class DatePickerFragment extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);


        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), (OnDateSetListener)getActivity(), year, month, day);
    }

}

public void onDateSet(DatePicker view, int year, int month, int day) {
    d.setDate(day);
    d.setMonth(month);
    d.setYear(year);
    int flags = 0;
    flags |= DateUtils.FORMAT_SHOW_DATE;
    flags |= DateUtils.FORMAT_SHOW_YEAR;
    String str = DateUtils.formatDateTime(this, d.getTime(), flags);
    date.setText(str);
}

实际上我不知道有什么问题请帮忙!

4

2 回答 2

3

这些调用已被弃用:

  d.setDate(day);
  d.setMonth(month);
  d.setYear(year);

并且没有按照你的设想去做。如果您跟踪并观察每次调用的 d 变化,您会惊讶地发现它只是将这些金额添加到自身中。

您应该改为执行以下操作:

Calendar c = Calendar.getInstance();
c.set(year, month, day);

SimpleDateFormat df = new SimpleDateFormat("d yyyy MMM");
String formattedDate = df.format(c.getTime());
date.setText(str);

您可以在此处查看更多日期格式选项:

http://developer.android.com/reference/java/text/SimpleDateFormat.html

于 2013-03-15T03:55:13.747 回答
-1

我实现这一目标的方式:

    public class AddEditCourseActivity extends Activity {
              private Button classStartBT;
        private DatePickerDialog mDatePickerDialog;
        private Calendar classStartDate;
        private SimpleDateFormat mDateFormatter;
        @Override
        protected void onCreate(Bundle savedInstanceState) {

                classStartDate = new GregorianCalendar();

                /** Initialize the mTimeFormatter object */
                mTimeFormatter = new SimpleDateFormat("yyyy dd, MM");

                classStartBT = (Button) findViewById(R.id.courseClassStartBT);

    classStartBT.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {


mDatePickerDialog = new DatePickerDialog(
                                MyActivity.this, mDateSetListener,
                                classStartDate.get(Calendar.YEAR),
                                classStartDate.get(Calendar.MONTH), 
                                classStartDate.get(Calendar.DAY_OF_WEEK));



                    mDatePickerDialog.show();

                } // end onClick
            });

        }


    private DatePickerDialog.OnDateSetListener mDateSetListener = new OnDateSetListener() {

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


                    classStartTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
                    classStartTime.set(Calendar.MINUTE, minute);

                    /**
                     * display the "class start time" on the buttons
                     */


    classStartBT.setText(mDateFormatter.format(classStartTime.getTime()));

            }
        };

    } // end activity
于 2013-03-15T03:05:28.170 回答