0

This is called from a button on a page using webview. It works great except for passing in the time. What am I doing wrong?

public void addToCalendar( final long startTime, String allday, final long endTime, String title, String myLocation) {
            // TODO Auto-generated method stub              
             Calendar cal = Calendar.getInstance();              
                Intent intent = new Intent(Intent.ACTION_EDIT);
                intent.setType("vnd.android.cursor.item/event");                    
                intent.putExtra("beginTime", cal.startTime);
                intent.putExtra("allDay", allday);
                intent.putExtra("rrule", "FREQ=YEARLY");
                intent.putExtra("endTime", cal.endTime);
                intent.putExtra("title", title);
                intent.putExtra("eventLocation",myLocation);
                startActivity(intent);
        }

I am getting the date 1/15/1970 10:45pm and 1/15/1970 10:45pm when i try passing in

How do I pass the date time?

This is mainly @Oriharel post How to add calendar events in Android?

Community
  • 1
  • 1
Rick
  • 1,153
  • 1
  • 18
  • 37
  • Doesn't this code give a syntax error on `intent.putExtra("beginTime", cal.startTime);`? The start time in the post is milliseconds from Jan 1 1970. – Dan S Aug 04 '11 at 22:14
  • Well thats the thing I have been just intent.putExtra("beginTime", startTime); however my dates and time just come out all funky. – Rick Aug 05 '11 at 00:50

3 Answers3

1

Ok I got it to work.

 public void addToCalendar(String startTime, String endTime, String allDay, String title, String myLocation, String descript) {
            // TODO Auto-generated method stub

            java.sql.Timestamp ts1 = java.sql.Timestamp.valueOf(startTime);
            java.sql.Timestamp ts2 = java.sql.Timestamp.valueOf(endTime);

            long tsTime1 = ts1.getTime();
            long tsTime2 = ts2.getTime();

                Intent intent = new Intent(Intent.ACTION_EDIT);
                intent.setType("vnd.android.cursor.item/event");

                intent.putExtra("beginTime", tsTime1);
                intent.putExtra("allDay", allDay);
                intent.putExtra("rrule", "FREQ=YEARLY");
                intent.putExtra("endTime", tsTime2);
                intent.putExtra("title", title);
                intent.putExtra("eventLocation",myLocation);
                intent.putExtra("description", descript);
                startActivity(intent);
        }
Rick
  • 1,153
  • 1
  • 18
  • 37
0

Posting alternative code as an aid to others on how this can be done. I've hard coded the date string to make the example more clear.

private long getEventDateAsLong(String dateToProcess ) {

    Date date = parseDate(dateToProcess); 
    Timestamp ts = new Timestamp(date.getTime());

    return ts.getTime(); 

}

private Date parseDate(String date) throws ParseException {     

    DateFormat formatter = DateFormat.getDateInstance(); 

        try {
    return formatter.parse(date);
    } catch (java.text.ParseException e) {
        e.printStackTrace();
    }
    return null;
} 

As I said earlier, I have hard coded two example dates as strings so you can see how it works:

String startDateToProcess = "July 3, 2013";
String endDateToProcess = "July 15, 2013";

and then wherever you are creating your calendar intent, your code should look something like this:

Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("beginTime", getEventDateAsLong(startDateToProcess));
intent.putExtra("endTime", getEventDateAsLong(endDateToProcess ));
startActivity(intent);
0

How do you use parameters startTime and endTime ? Actually you use a new Calendar instance, pointing on now.

Regards, Stéphane

Snicolas
  • 37,840
  • 15
  • 114
  • 173
  • "pointing on now" === what does this mean? was there to be a link here? And I see the new api http://code.google.com/apis/calendar/data/2.0/developers_guide_java.html#CreatingSingle BUT... STILL how do you pass in the date time I don't see the format. – Rick Aug 05 '11 at 00:51
  • I was just saying that you didn't use the parameters of your method and used a new Calendar Object. Anyhow, what are startTime and endTime, how do you get their values ? – Snicolas Aug 05 '11 at 02:54
  • i pass them in via a javascript wrapper in a webview. right now Im trying "2012-08-01T14:00:00" – Rick Aug 05 '11 at 03:00
  • 1
    they should be long values, so what are their values, as longs. – Snicolas Aug 05 '11 at 03:02