user3085327
user3085327

Reputation: 13

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

Upvotes: 1

Views: 3031

Answers (2)

amps
amps

Reputation: 63

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

Upvotes: 1

user773737
user773737

Reputation:

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

Upvotes: 0

Related Questions