如何从Android日历获取日期?

时间:2013-12-16 23:12:55

标签: java android date calendar

请帮我从日历中获取日期。想法是用户必须选择日期。之后,日期又回到了主要的Actiity。 我正在尝试这样做:

Calendar today = Calendar.getInstance();

Uri uriCalendar = Uri.parse("content://com.android.calendar/time/" + String.valueOf(today.getTimeInMillis()));
Intent intentCalendar = new Intent(Intent.ACTION_DATE_CHANGED,uriCalendar);            
startActivity(intentCalendar);

但它只打开日历,我无法选择日期。谢谢=)

2 个答案:

答案 0 :(得分:1)

我正在使用它,它对我有用 另外一个提示,对于复杂的日期操作,我更喜欢Joda Time over Calendar。我真的推荐它。

  change_date_but = (Button) findViewById(R.id.c_change_button_id);
    change_date_but.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // DatePickerDialog DPD = new DatePickerDialog(this,
            // mDatesetlistener, mYear, mMonth, mDay);
            DatePickerDialog DPD = new DatePickerDialog(
                    AddEditChildren.this, mDateSetListener, mYear, mMonth,
                    mDay);
                DPD.show();

        }
    });

    private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        mYear = year;
        mMonth = monthOfYear;
        mDay = dayOfMonth;
        updateDisplay();
    }
};

//我用它来显示一个漂亮的日期格式

private void updateDisplay() {
    // TODO Auto-generated method stub
    if(mMonth+1<10 && mDay >10)
        date.setText(new StringBuilder()
        .append(mYear).append("-").append("0").append(mMonth + 1).append("-").append(mDay));
    else if(mMonth+1>10 && mDay<10)
        date.setText(new StringBuilder()
        // Month is 0 based so add 1
        .append(mYear).append("-").append(mMonth + 1).append("-").append("0").append(mDay));

    else if(mMonth+1<10 && mDay<10)
        date.setText(new StringBuilder()
        // Month is 0 based so add 1
        .append(mYear).append("-").append("0").append(mMonth + 1).append("-").append("0").append(mDay));
    else
        date.setText(new StringBuilder()
            // Month is 0 based so add 1
        .append(mYear).append("-").append(mMonth + 1).append("-").append(mDay));
}

答案 1 :(得分:0)

试试Date Picker。不过,这不是传统的日历。