Create Calendar Gadget using the Google Data Java client library.


Create Calendar Gadget (WebContent event) using the Google Data Java client library.
  public DateTime getToday() {

  Date today = new Date();

  DateTime datetime = new DateTime(today);

  datetime.setDateOnly(true);

  return datetime;  
}

public DateTime getTomorrow() {

  long oneDay = 24 * 60 * 60 * 1000;

  Date today = new Date();

  DateTime datetime = new DateTime(new Date(today.getTime() + oneDay));

  datetime.setDateOnly(true);

  return datetime;
}

public void createWebContent() throws Exception {
  URL feedUrl = new URL("http://www.google.com/calendar/feeds/default/private/full");

  CalendarEventEntry entry = new CalendarEventEntry();

  entry.setTitle(new PlainTextConstruct("create web content"));
  entry.setContent(new PlainTextConstruct("This event is created by Java client library"));

  DateTime startTime = getToday();
  DateTime endTime = getTomorrow();

  When eventTimes = new When();
  eventTimes.setStartTime(startTime);
  eventTimes.setEndTime(endTime);
  entry.addTime(eventTimes);

  WebContent wc = new WebContent();

  wc.setTitle("title");
  wc.setType("text/html");
  wc.setIcon("http://www.google.com/intl/en_ALL/images/logo.gif");
  wc.setUrl("http://www.google.com");
  wc.setWidth("800");
  wc.setHeight("600");

  entry.setWebContent(wc);

  // Send the request and receive the response:

  calendarService.insert(feedUrl, entry);
  System.out.println("Calendar Gadget is created");
}

6 comments: