1

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

Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
user3085327
  • 13
  • 1
  • 6
  • Do you have any code that captures the user's selection? –  Dec 16 '13 at 23:14
  • No. How can i make it? – user3085327 Dec 16 '13 at 23:16
  • Look at this: http://developer.android.com/guide/topics/ui/controls/pickers.html It's not really a calendar, but you can pick a date. Also, see this: http://stackoverflow.com/questions/3712710/android-calendar-view-for-date-picker –  Dec 16 '13 at 23:20

2 Answers2

1

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));
}
amps
  • 63
  • 7
  • You don't need the StringBuilder in every branch. Just concatenate with "+". The compiler will optimize and it will be much better readable. – Moritz Aug 24 '15 at 14:27
0

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