简体   繁体   中英

How to get date from android calendar?

Please, help me get date from calendar. Idea is that user must choose date. After that the date has returned to the main Actiity. I'm trying do like this:

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

but it only open calendar and i cannot choose date. Thanks =)

I am using this and it works for me One additional tip, for complex dates manipulation, I prefer Joda Time over Calendar. I really recommend it.

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

//I use this to show a nice date format

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

Try the Date Picker . It's not a traditional calendar, though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM