简体   繁体   中英

How to get event from Google Calendar in Java?

I use this code for getting Google calendars list:

// Create a CalenderService and authenticate
CalendarService myService = new CalendarService("exampleCo-exampleApp-1");
myService.setUserCredentials("j...@gmail.com", "mypassword");

// Send the request and print the response
URL feedUrl = new URL("https://www.google.com/calendar/feeds/default/allcalendars/full");
CalendarFeed resultFeed = myService.getFeed(feedUrl, CalendarFeed.class);
System.out.println("Your calendars:");
System.out.println();
for (int i = 0; i < resultFeed.getEntries().size(); i++) {
  CalendarEntry entry = resultFeed.getEntries().get(i);
  System.out.println("\t" + entry.getTitle().getPlainText());
}

This code return me calendars list, but I need to get all events from this calendar about today. How can I do that?

If you are using the sample program to start with please use ..../private/full to get the events from Calendar.

For example:

"https://www.google.com/calendar/feeds/xxx@gmail.com/private/full");

You can also use a this default URL, instead of specifying xxx@gmail.com,

"https://www.google.com/calendar/feeds/default/private/full"

And I think you should use CalendarEventFeed Object instead of CalendarFeed See this code snippet below:

CalendarQuery myQuery = new CalendarQuery(feedUrl);
myService.setUserCredentials(p.getgUser(), p.getgPassword());

 DateTime dt = new DateTime();
 myQuery.setMinimumStartTime(DateTime.now());
 myQuery.setMaximumStartTime(dt);

CalendarEventFeed resultFeed = myService.query(myQuery, CalendarEventFeed.class);

Then traverse the contents of resultFeed

.....

CalendarEventFeed resultFeed = calendarData

for (int i = 0; i<calendarData.getEntries().size(); i++) {
          CalendarEventEntry entry = calendarData.getEntries().get(i);

.....

Assuming you're using the official GData API:

Try using the getFeed(Query query, …) overload with a CalendarQuery - that seems to let you specify the range for the start time of an event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM