Google Calendar, 一个不错的东西, 用她可以方便地进行日程管理,组织,分享等, 甚至还包括邮件提醒, 手机短信提醒 等.
参考Google Calender JAVA API(V2): https://developers.google.com/google-apps/calendar/v2/developers_guide_java
下面开始 使用JAVA 调用 Google Calender, 主要包括 Event add/edit/delete, Calendar add/delete/list
1.添加 gdata 的依赖:
在此提供两种方式添加依赖, 使用MAVEN与直接使用jar文件.
1.1 使用Maven
在POM文件中添加以下依赖:
<dependency> <groupId>org.openengsb.wrapped</groupId> <artifactId>com.google.gdata-calendar</artifactId> <version>1.41.5.w1</version> </dependency>
1.2 使用jar文件
如果不使用MAVEN, 则到http://code.google.com/p/gdata-java-client/downloads/list 直接去下载 相应的jar文件, 加入到 classpath中即可.
2.主要代码:
>>> 获取 CalendarService
CalendarService 是API的核心类, 所有操作都是调用该类的具体方法实现, 如getFeed,insert等,
创建该类的实例必须 指定 GMAIL 用户名,密码. 代码如下:
private static CalendarService createCalendarService(String gmailName, String gmailPass) throws AuthenticationException { CalendarService calendarService = new CalendarService("myCalendarService"); calendarService.setUserCredentials(gmailName, gmailPass); return calendarService; }
注: myCalendarService 只是一个名字,没实际含义
2.1 Event add/edit/delete
Event, 具体指一个Calendar 里面的一个 活动,
* add event
CalendarEventEntry myEntry = new CalendarEventEntry(); long millis = System.currentTimeMillis(); myEntry.setTitle(new PlainTextConstruct("A new Event Entry > " + millis)); myEntry.setContent(new PlainTextConstruct("I'm content")); TimeZone timeZone = TimeZone.getDefault(); DateTime startTime = new DateTime(new Date(), timeZone); DateTime endTime = new DateTime(new Date(millis + 10000000), timeZone); When eventTimes = new When(); eventTimes.setStartTime(startTime); eventTimes.setEndTime(endTime); myEntry.addTime(eventTimes); URL postUrl = new URL("https://www.google.com/calendar/feeds/[email protected]/private/full"); CalendarEventEntry eventEntry = calendarService.insert(postUrl, myEntry); String href = eventEntry.getEditLink().getHref(); System.out.println("Edit URL: " + href);
> Event对应的实体名叫 CalendarEventEntry. 在使用时必须 创建一个实例.
> Call setTitle, setContent 设置 Event的标题与内容.
> When类型用于设置 Event的开始时间(startTime)与结束时间(endTime), 在使用时最好指定TimeZone.
> 注意 URL的红色部分([email protected]) 必须替换为 创建 CalendarService的 gmail 帐户名.
> 调用 CalendarService的 insert 方法即可 add 一个新的Event.
> 注意 在添加完成后的那行代码
String href = eventEntry.getEditLink().getHref();这儿的 href 变量为 添加的Event的连接地址, 需要保存,用于在编辑或删除时从Google Calendar获取 CalendarEventEntry 实例.一个示例:
https://www.google.com/calendar/feeds/my.test%40gmail.com/private/full/9dfcab6hsqb7ld4ppc9quuch88
添加后的 Google Calendar 页面如下:
* edit/delete event
对于edit/delete 操作的第一步是如何获取对应 的 CalendarEventEntry 实例. 具体步骤为:
1). 用gmail创建创建 CalendarService 实例
2). 通过添加 Event 获取的 连接地址(href) 调用CalendarService.getEntry 获取对应的CalendarEventEntry 实例.
3). 对CalendarEventEntry进行相应的edit,delete.
edit code:
//get URL getUrl = new URL(href); CalendarEventEntry entry = calendarService.getEntry(getUrl, CalendarEventEntry.class); //edit entry.setTitle(new PlainTextConstruct("I'm a new title")); URL editUrl = new URL(entry.getEditLink().getHref()); CalendarEventEntry updatedEntry = calendarService.update(editUrl, entry);
//get URL getUrl = new URL(href); CalendarEventEntry entry = calendarService.getEntry(getUrl, CalendarEventEntry.class); //del entry.delete();
2.2 Calendar add/delete/list
在Google Calendar 中,默认有一个以 邮箱名命名的日历, 该日历可以获取,修改,但不能删除(其他添加的日历是可以删除的).
* add calendar
CalendarEntry entry = new CalendarEntry(); entry.setTitle(new PlainTextConstruct("A new Calendar")); entry.setSummary(new PlainTextConstruct("I have a new Calendar.")); entry.setTimeZone(new TimeZoneProperty("Asia/Shanghai")); entry.setHidden(HiddenProperty.FALSE); entry.setColor(new ColorProperty("#2952A3")); entry.addLocation(new Where("", "", "Shanghai")); URL postUrl = new URL("https://www.google.com/entry/feeds/default/owncalendars/full"); CalendarEntry returnedCalendar = calendarService.insert(postUrl, entry); System.out.println("ID =" + returnedCalendar.getId());
delete
URL feedUrl = new URL("https://www.google.com/calendar/feeds/default/owncalendars/full"); CalendarFeed resultFeed = calendarService.getFeed(feedUrl, CalendarFeed.class); List<CalendarEntry> entries = resultFeed.getEntries(); for (CalendarEntry entry : entries) { System.out.println("Delete > " + entry.getId() + ", " + entry.getTitle().getPlainText()); try { entry.delete(); } catch (InvalidEntryException e) { e.printStackTrace(); } }
edit
URL feedUrl = new URL("https://www.google.com/calendar/feeds/default/owncalendars/full"); CalendarFeed resultFeed = calendarService.getFeed(feedUrl, CalendarFeed.class); List<CalendarEntry> entries = resultFeed.getEntries(); for (CalendarEntry entry : entries) { entry.setColor(new ColorProperty("#2952A3")); String href = entry.getEditLink().getHref(); URL edUrl = new URL(href); calendarService.update(edUrl, entry); }
在真实的使用环境中.对Calendar的操作一般比较少, 更多 的是对 Event的操作.