Java using Google Calendar__java

Source: Internet
Author: User


Google calendar, a good thing, with her can easily schedule management, organization, sharing, etc., even include e-mail reminders, SMS reminders, etc.


Reference Google calender JAVA API (V2): Https://developers.google.com/google-apps/calendar/v2/developers_guide_java


The following begins using Java to invoke Google calender, mainly including Event Add/edit/delete, Calendar add/delete/list


1. Add gdata dependencies:

Here are two ways to add dependencies, using MAVEN and using the jar file directly.

1.1 Using Maven

Add the following dependencies in the Pom file:

        <dependency>
            <groupId>org.openengsb.wrapped</groupId>
            <artifactId> com.google.gdata-calendar</artifactid>
            <version>1.41.5.w1</version>
        </dependency >


1.2 Using JAR files

If you do not use Maven, go to http://code.google.com/p/gdata-java-client/downloads/list directly to download the appropriate jar file, add to Classpath.


2. Main code:

>>> Get Calendarservice

Calendarservice is the core class of the API, and all operations are implemented in a specific way that calls the class, such as Getfeed,insert,

To create an instance of this class, you must specify the GMAIL username and password. The code is as follows:

    private static Calendarservice Createcalendarservice (String gmailname, String gmailpass) throws authenticationexception {
        Calendarservice calendarservice = new Calendarservice ("Mycalendarservice");
        Calendarservice.setusercredentials (Gmailname, gmailpass);
        return calendarservice;
    }

Note: Mycalendarservice is just a name, no real meaning


2.1 Event Add/edit/delete

event, specifically a calendar inside an activity,

* 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/my.test@gmail.com/private/full");
        Calendarevententry evententry = Calendarservice.insert (PostURL, myentry);

        String href = Evententry.geteditlink (). Gethref ();
        System.out.println ("Edit URL:" + href);

Description

> event corresponds to the entity named Calendarevententry. You must create an instance when you use it.

> Call Settitle, SetContent set the event title and content.

The > when type is used to set the start time (starttime) and End Time (endtime) of an event, preferably when used with the timezone specified.

> Note the red portion of the URL (my.test@gmail.com) must be replaced with the name of the Gmail account that created the Calendarservice.

> Invoke the Calendarservice Insert method to add a new event.

> Note the line of code after the completion of the add

String href = Evententry.geteditlink (). Gethref ();
The href variable here is the connection address for the added event, which needs to be saved to get the Calendarevententry instance from Google Calendar when editing or deleting. An example:

Https://www.google.com/calendar/feeds/my.test%40gmail.com/private/full/9dfcab6hsqb7ld4ppc9quuch88


The following Google Calendar pages are added:




* Edit/delete Event

The first step for the edit/delete operation is how to obtain the corresponding Calendarevententry instance. The specific steps are:

1. Create a Calendarservice instance with Gmail

2) Call Calendarservice.getentry to get the corresponding Calendarevententry instance by adding the connection address (HREF) obtained by the Event.

3). The corresponding edit,delete of the calendarevententry.

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);

Delete code:

        Get
        url getUrl = new url (href);
        Calendarevententry entry = Calendarservice.getentry (GETURL, calendarevententry.class);
        Del
        entry.delete ();



2.2 Calendar Add/delete/list


In Google Calendar, there is a calendar named after the mailbox name by default, which can be obtained, modified, but not deleted (other added calendars can be deleted).


* 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 ());

* Edit/delete Calendar (example in this Code reference API)

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 (). Getp Laintext ());
            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);
        }


In a real-life environment. The operation of the calendar is generally relatively small, more is the operation of the event.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.