Math
Math

Reputation: 187

Google Calendars API - setTimeMax causing error

I'm trying to retrieve a list of events from a google calendar, using the Java api (jar version v3-rev9-1.7.0-beta)

This code works fine

Events e = service.events().list("primary").
                setMaxResults(3).
                execute(); 

where service is a Calendar object.

However, if I add a setTimeMin or setTimeMax parameter, like this

Date now = new java.util.Date();
Events e = service.events().list("primary").
                setTimeMin(new DateTime(now)).
                setMaxResults(3).
                execute(); 

it returns a failure message, "Bad Request".

(note that as of this version, the setTime functions take a google DateTime object. I've also tried with the previous version of the jar, which takes a string, but received the same message).

So I was just wondering if anyone has successfully used these functions - perhaps they're not supposed to be called in this manner? Is there a way to get more detail back on the error?

Thanks :)

Upvotes: 2

Views: 909

Answers (2)

sriram
sriram

Reputation: 66

DateTime startTime = new DateTime(new Date(), TimeZone.getDefault());

Sorts the problem

Upvotes: 5

Andrew Taylor
Andrew Taylor

Reputation: 1408

I also encountered this. It seems the format of the DateTime.toString() or DateTime.toStringRfc3339() methods are incorrect as input to setTimeMin().

The DateTime.toString() format is:
2012-07-04T21:02:16.590
yyyy-MM-dd'T'HH:mm:ss.SSS (SimpleDateFormat notation)

The format which it expects seems to be close to xsd:datetime format (whatever that is):
2012-07-04T21:02:16Z (zulu, gmt)
2012-07-04T21:02:16-07:00 (mst, -7h)
2012-07-04T21:02:16-0700 (it also works without the colon in the timezone)
yyyy-MM-dd'T'HH:mm:ssZ (SimpleDateFormat)

Formatting can be done with a SimpleDateFormat:

SimpleDateFormat FMT_TIME=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
String fromTimeStr=FMT_TIME.format(new Date());
Events evts = client.events().list(cal.getUid()).setTimeMin(fromTimeStr)
             .execute();

Now, since I'm using the older API, I'm not sure how this would be done if the only method is setTimeMin(DateTime), but this should get you closer.

The Google documentation or source should mention this somewhere.

Upvotes: 3

Related Questions