Igor
Igor

Reputation: 205

Calendar in android

I´m doing a Calendar with Android with this code that I have find here.

Calendar cal = Calendar.getInstance();              
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("beginTime", cal.getTimeInMillis());
intent.putExtra("allDay", false);
intent.putExtra("rrule", "FREQ=DAILY");
intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
intent.putExtra("title", "A Test Event from android app");
startActivity(intent);

in this sentence intent.putExtra("beginTime", cal.getTimeInMillis()); we put our begin time but how can I do if I want to put my begin time within 3 months for example because if I put cal.getTimeInMillis()+10000000000(a lot of time) the event day doesn´t change.

Thank you and sorry but my english is bad.

Upvotes: 1

Views: 2752

Answers (3)

kannappan
kannappan

Reputation: 2250

If you want to create a new calendar event, try this code:

Intent intent = new Intent(Intent.ACTION_EDIT);  
intent.setType("vnd.android.cursor.item/event");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(intent,0);

If you want to edit the calendar event, try this code:

Uri uri = Uri.parse("content://calendar/events");
long eventId = mId.get(mPosition);
Uri newuri = ContentUris.withAppendedId(uri, eventId);
Intent intent = new Intent(Intent.ACTION_VIEW,newuri);
Cursor cursor = getContentResolver().query(newuri, new String[]{"dtstart","dtend",},null, null, null);
if(cursor.getCount()>0)
{
    cursor.moveToFirst();
    intent.putExtra("beginTime", cursor.getLong(cursor.getColumnIndex("dtstart")));
    intent.putExtra("endTime",  cursor.getLong(cursor.getColumnIndex("dtend")));
}
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(intent,1);

Upvotes: 0

Nitin
Nitin

Reputation: 1451

int componentTimeToTimestamp(int year, int month, int day, int hour, int minute) {

    Calendar c = Calendar.getInstance();
    c.set(Calendar.YEAR, year);
    c.set(Calendar.MONTH, month);
    c.set(Calendar.DAY_OF_MONTH, day);
    c.set(Calendar.HOUR, hour);
    c.set(Calendar.MINUTE, minute);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0);

    return (int) (c.getTimeInMillis() / 1000L);
}

Upvotes: 0

Dayerman
Dayerman

Reputation: 4003

You can use the method of the Class Calendar to add the amount of time you want.

// Add 10 months to the calendar
   cal.add(Calendar.MONTH, 10);

BTW This is not a proper Android question.

Upvotes: 1

Related Questions