How to manage calendar events with the Nylas Java SDK

How to manage calendar events with the Nylas Java SDK

18 min read

Scheduling, organizing, or attending events, calendars are critical for companies and individuals alike. Nylas makes it possible to bring calendaring features to your application with our universal Calendar API that works across most major providers.

To make things even easier for Java developers, we offer the Nylas Java SDK. Today we will review how to manage calendar events with Java using the SDK.

If you’re looking for NodeJs read How to Manage Calendar Events with the Nylas Node SDK; for Python read How to Manage Calendar Events with the Nylas Python SDK; or Ruby read How to Manage Calendar Events with the Nylas Ruby SDK.

Is your system ready?

If you’re new to Java or Nylas, we recommend reading the post How to Send Emails with the Nylas Java SDK where everything is explained in detail.

What are we going to talk about?

Reading calendars

Let’s start by reading a list of calendars, looking at the calendar id, name, and description. We are going to create a project called Reading_Calendars with the main class called ReadCalendars.

This will be our pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <groupId>Nylas</groupId>
   <artifactId>Reading_Calendars</artifactId>
   <version>1.0-SNAPSHOT</version>

   <properties>
       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
       <maven.compiler.source>17</maven.compiler.source>
       <maven.compiler.target>17</maven.compiler.target>
   </properties>

   <dependencies>
       <dependency>
           <groupId>com.nylas.sdk</groupId>
           <artifactId>nylas-java-sdk</artifactId>
           <version>1.16.0</version>
       </dependency>
       <dependency>
           <groupId>org.slf4j</groupId>
           <artifactId>slf4j-jdk14</artifactId>
           <version>1.7.25</version>
       </dependency>
       <dependency>
           <groupId>io.github.cdimascio</groupId>
           <artifactId>dotenv-java</artifactId>
           <version>2.2.4</version>
       </dependency>
   </dependencies>

   <build>
       <pluginManagement>
           <plugins>
               <plugin>
                   <!-- Build an executable JAR -->
                   <groupId>org.apache.maven.plugins</groupId>
                   <artifactId>maven-jar-plugin</artifactId>
                   <version>3.1.0</version>
                   <configuration>
                       <archive>
                           <manifest>
                               <addClasspath>true</addClasspath>
                               <mainClass>ReadCalendars</mainClass>
                           </manifest>
                       </archive>
                   </configuration>
               </plugin>
               <plugin>
                   <artifactId>maven-assembly-plugin</artifactId>
                   <configuration>
                       <archive>
                           <manifest>
                               <mainClass>ReadCalendars</mainClass>
                           </manifest>
                       </archive>
                       <descriptorRefs>
                           <descriptorRef>jar-with-dependencies</descriptorRef>
                       </descriptorRefs>
                   </configuration>
               </plugin>
               <plugin>
                   <groupId>org.codehaus.mojo</groupId>
                   <artifactId>exec-maven-plugin</artifactId>
                   <version>1.2.1</version>
                   <executions>
                       <execution>
                           <phase>package</phase>
                           <goals>
                               <goal>java</goal>
                           </goals>
                       </execution>
                   </executions>
                   <configuration>
                       <mainClass>ReadCalendars</mainClass>
                       <cleanupDaemonThreads>false</cleanupDaemonThreads>
                   </configuration>
               </plugin>
           </plugins>
       </pluginManagement>
   </build>

</project>

And here’s our source code:

//Import Java Utilities
import java.io.IOException;
import java.util.List;

//Import Nylas Packages
import com.nylas.*;

//Import DotEnv to handle .env files
import io.github.cdimascio.dotenv.Dotenv;
import io.github.cdimascio.dotenv.DotenvException;

//Import DotEnv to handle .env files
import io.github.cdimascio.dotenv.Dotenv;
import io.github.cdimascio.dotenv.DotenvException;

public class ReadCalendars {
    public static void main(String[] args) throws RequestFailedException, 
                                                  IOException {
        Dotenv dotenv = Dotenv.load();
        // Create the client object
        NylasClient client = new NylasClient();
        // Connect it to Nylas using the Access Token from the .env file
        NylasAccount account = client.account(dotenv.get("ACCESS_TOKEN"));

        // Access the Calendars endpoint
        Calendars calendars = account.calendars();
        // Read all calendars
        RemoteCollection<Calendar> calendar_list = calendars.list();

        // Loop through calendars
        for (Calendar calendar : calendar_list){
            System.out.println("Id: " + calendar.getId() +
                               " | Name: " + calendar.getName() +
                               " | Description: " + 
                                 calendar.getDescription());
        }
    }
}

To run our project, we need to compile it first. In order to do this,  we can open a Terminal window, go to our project’s root folder and type the following maven command:

$ mvn package
Compiling Reading Calendar

Once it’s done, we can run this command to execute our application:

$ mvn exec:java -Dexec.mainClass="ReadCalendars"
Reading Calendars

All calendars will be returned along with their id, name and description.

Reading calendar events

One advantage of reading a Calendar is seeing events in our schedule, so that’s what we’re going to do now. We’re going to choose a Calendar and read its related events using Java. For this, we need to grab the Id of one of the calendars and add it to our .env file. We’re going to create a new project called Read_Events with the main class called ReadEvents.

Here’s our pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <groupId>Nylas</groupId>
   <artifactId>Read_Events</artifactId>
   <version>1.0-SNAPSHOT</version>

   <properties>
       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
       <maven.compiler.source>17</maven.compiler.source>
       <maven.compiler.target>17</maven.compiler.target>
   </properties>

   <dependencies>
       <dependency>
           <groupId>com.nylas.sdk</groupId>
           <artifactId>nylas-java-sdk</artifactId>
           <version>1.16.0</version>
       </dependency>
       <dependency>
           <groupId>org.slf4j</groupId>
           <artifactId>slf4j-jdk14</artifactId>
           <version>1.7.25</version>
       </dependency>
       <dependency>
           <groupId>io.github.cdimascio</groupId>
           <artifactId>dotenv-java</artifactId>
           <version>2.2.4</version>
       </dependency>
   </dependencies>

   <build>
       <pluginManagement>
           <plugins>
               <plugin>
                   <!-- Build an executable JAR -->
                   <groupId>org.apache.maven.plugins</groupId>
                   <artifactId>maven-jar-plugin</artifactId>
                   <version>3.1.0</version>
                   <configuration>
                       <archive>
                           <manifest>
                               <addClasspath>true</addClasspath>
                               <mainClass>ReadEvents</mainClass>
                           </manifest>
                       </archive>
                   </configuration>
               </plugin>
               <plugin>
                   <artifactId>maven-assembly-plugin</artifactId>
                   <configuration>
                       <archive>
                           <manifest>
                               <mainClass>ReadEvents</mainClass>
                           </manifest>
                       </archive>
                       <descriptorRefs>
                           <descriptorRef>jar-with-dependencies</descriptorRef>
                       </descriptorRefs>
                   </configuration>
               </plugin>
               <plugin>
                   <groupId>org.codehaus.mojo</groupId>
                   <artifactId>exec-maven-plugin</artifactId>
                   <version>1.2.1</version>
                   <executions>
                       <execution>
                           <phase>package</phase>
                           <goals>
                               <goal>java</goal>
                           </goals>
                       </execution>
                   </executions>
                   <configuration>
                       <mainClass>ReadEvents</mainClass>
                       <cleanupDaemonThreads>false</cleanupDaemonThreads>
                   </configuration>
               </plugin>
           </plugins>
       </pluginManagement>
   </build>

</project>

And here’s our source code:

// Import Java Utilities
import java.io.IOException;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;

// Import Nylas Packages
import com.nylas.RequestFailedException;
import com.nylas.NylasAccount;
import com.nylas.NylasClient;
import com.nylas.Event;
import com.nylas.EventQuery;
import com.nylas.Events;
import com.nylas.RemoteCollection;
import com.nylas.Participant;

//Import DotEnv to handle .env files
import io.github.cdimascio.dotenv.Dotenv;
import io.github.cdimascio.dotenv.DotenvException;

public class ReadEvents {
   public static void main(String[] args) throws RequestFailedException, 
                                                 IOException {
       Dotenv dotenv = Dotenv.load();
       // Create the client object
       NylasClient client = new NylasClient();
       // Connect it to Nylas using the Access Token from the .env file
       NylasAccount account = client.account(dotenv.get("ACCESS_TOKEN"));

       // Access the Events endpoint
       Events events = account.events();
       // Get the first 3 events that happen between 
       // today and next month
       RemoteCollection<Event> events_list = events.list(new EventQuery()
                               .calendarId(dotenv.get("CALENDAR_ID")).limit(3)
                               .startsAfter(Instant.now())
                               .endsBefore(Instant.now().plus(30, 
                                           ChronoUnit.DAYS )));
       // Loop through the events
       for (Event event : events_list){
           System.out.print("Title: " + event.getTitle());
           // Dates are handled differently depending on the event type
           switch (event.getWhen().getObjectType()) {
               case "date":
                   Date date = (Date) event.getWhen();
                   System.out.println(" | The date of the event " + 
                                       date.toString());
                   break;
               case "timespan":
                   Event.Timespan timespan = (Event.Timespan) event.getWhen();
                   System.out.println(" | The time of the event is from: " + 
                                      timespan.getStartTime() +
                                      " to " + timespan.getEndTime());
                   break;
           }
           System.out.print(" | Participants: ");
           // Get all participants
           for(Participant participant : event.getParticipants()){
               System.out.println(" Email: " + participant.getEmail() +
                                  " Name: " + participant.getName() +
                                  " Status: " + participant.getStatus());
           }
           System.out.println("");
       }
   }
}

In order to run our project, we need to compile it first. In order to do this,  we can open a Terminal window, go to our project’s root folder and type the following maven command:

$ mvn package

Once it’s done, we can run this command to execute our application:

$ mvn exec:java -Dexec.mainClass="ReadEvents"
Reading Calendar Events

By getting participants and their status, we can create a follow-up plan.

Creating calendar events

Reading events is nice and everything, but creating events is what we should look forward to. Let’s create a new project called Create_Events with the main class called CreateEvents.

Here’s our pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <groupId>Nylas</groupId>
   <artifactId>Create_Events</artifactId>
   <version>1.0-SNAPSHOT</version>

   <properties>
       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
       <maven.compiler.source>17</maven.compiler.source>
       <maven.compiler.target>17</maven.compiler.target>
   </properties>

   <dependencies>
       <dependency>
           <groupId>com.nylas.sdk</groupId>
           <artifactId>nylas-java-sdk</artifactId>
           <version>1.16.0</version>
       </dependency>
       <dependency>
           <groupId>org.slf4j</groupId>
           <artifactId>slf4j-jdk14</artifactId>
           <version>1.7.25</version>
       </dependency>
       <dependency>
           <groupId>io.github.cdimascio</groupId>
           <artifactId>dotenv-java</artifactId>
           <version>2.2.4</version>
       </dependency>
   </dependencies>

   <build>
       <pluginManagement>
           <plugins>
               <plugin>
                   <!-- Build an executable JAR -->
                   <groupId>org.apache.maven.plugins</groupId>
                   <artifactId>maven-jar-plugin</artifactId>
                   <version>3.1.0</version>
                   <configuration>
                       <archive>
                           <manifest>
                               <addClasspath>true</addClasspath>
                               <mainClass>CreateEvents</mainClass>
                           </manifest>
                       </archive>
                   </configuration>
               </plugin>
               <plugin>
                   <artifactId>maven-assembly-plugin</artifactId>
                   <configuration>
                       <archive>
                           <manifest>
                               <mainClass>CreateEvents</mainClass>
                           </manifest>
                       </archive>
                       <descriptorRefs>
                           <descriptorRef>jar-with-dependencies</descriptorRef>
                       </descriptorRefs>
                   </configuration>
               </plugin>
               <plugin>
                   <groupId>org.codehaus.mojo</groupId>
                   <artifactId>exec-maven-plugin</artifactId>
                   <version>1.2.1</version>
                   <executions>
                       <execution>
                           <phase>package</phase>
                           <goals>
                               <goal>java</goal>
                           </goals>
                       </execution>
                   </executions>
                   <configuration>
                       <mainClass>CreateEvents</mainClass>
                       <cleanupDaemonThreads>false</cleanupDaemonThreads>
                   </configuration>
               </plugin>
           </plugins>
       </pluginManagement>
   </build>

</project>

And here’s our source code:

//Import Java Utilities
import java.io.IOException;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;

// Import Nylas Packages
import com.nylas.RequestFailedException;
import com.nylas.NylasAccount;
import com.nylas.NylasClient;
import com.nylas.Event;
import com.nylas.Participant;

//Import DotEnv to handle .env files
import io.github.cdimascio.dotenv.Dotenv;
import io.github.cdimascio.dotenv.DotenvException;

public class CreateEvents {
   public static void main(String[] args) throws RequestFailedException, 
                                                 IOException {
       Dotenv dotenv = Dotenv.load();
       // Create the client object
       NylasClient client = new NylasClient();
       // Connect it to Nylas using the Access Token from the .env file
       NylasAccount account = client.account(dotenv.get("ACCESS_TOKEN"));

       // Get today's date
       LocalDate today = LocalDate.now();
       // Set time. As we're using UTC we need to add the hours 
       // in difference
       // from our own Timezone
       Instant sixPmUtc = today.atTime(13, 0).toInstant(ZoneOffset.UTC);
       // Set the date and time for the event. 
       // We add 30 minutes to the starting time
       Event event = new Event(dotenv.get("CALENDAR_ID"),
                               new Event.Timespan(sixPmUtc,
                                       sixPmUtc.plus(30, 
                                       ChronoUnit.MINUTES)));
       // Set Title, Location and Description
       event.setTitle("Let's learn some Nylas JAVA SDK!");
       event.setLocation("Blag's Den!");
       event.setDescription("Using the Nylas API with the 
                             Java SDK is easy. Come join us!");

       // Add participants
       event.setParticipants(
           Arrays.asList(new Participant("<YOUR_EMAIL_ADDRESS>").name("Blag"))
       );

       // Create the event. Are we notifying participants? Yes.
       Event event_created = account.events().create(event, true);
       if(event_created.getId() != null){
           System.out.println("Event created successfully");
       }else{
           System.out.println("There was an error creating the event");
       }

   }
}

To run our project, we need to compile it first. To do this, we can open a Terminal window, go to our project’s root folder and type the following maven command:

$ mvn package

Once it’s done, we can run this command to execute our application:

$ mvn exec:java -Dexec.mainClass="CreateEvents"
Creating Calendar Events

And we confirm by opening up the event invitation.

Event invitation

Creating events is both a fast and easy task, when using the Nylas SDK.

Updating calendar events

Sometimes, we might realize that we forgot to add something to the invite, or maybe the time of our event has changed. We’ll need to update the event we just created. 

To do that, we need to have the event id. If we modify our ReadEvents project slightly, we can easily get the id.

Just add this line:

// Get the event id
System.out.print("Id: " + event.getId() + " | ";
System.out.print("Title: " + event.getTitle());
Reading Calendar Events

Once we have the Id of the event that we want, we can create a new project called Update_Events with the main class called UpdateEvents.

Here’s our pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <groupId>Nylas</groupId>
   <artifactId>Update_Events</artifactId>
   <version>1.0-SNAPSHOT</version>

   <properties>
       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
       <maven.compiler.source>17</maven.compiler.source>
       <maven.compiler.target>17</maven.compiler.target>
   </properties>

   <dependencies>
       <dependency>
           <groupId>com.nylas.sdk</groupId>
           <artifactId>nylas-java-sdk</artifactId>
           <version>1.16.0</version>
       </dependency>
       <dependency>
           <groupId>org.slf4j</groupId>
           <artifactId>slf4j-jdk14</artifactId>
           <version>1.7.25</version>
       </dependency>
       <dependency>
           <groupId>io.github.cdimascio</groupId>
           <artifactId>dotenv-java</artifactId>
           <version>2.2.4</version>
       </dependency>
   </dependencies>

   <build>
       <pluginManagement>
           <plugins>
               <plugin>
                   <!-- Build an executable JAR -->
                   <groupId>org.apache.maven.plugins</groupId>
                   <artifactId>maven-jar-plugin</artifactId>
                   <version>3.1.0</version>
                   <configuration>
                       <archive>
                           <manifest>
                               <addClasspath>true</addClasspath>
                               <mainClass>UpdateEvents</mainClass>
                           </manifest>
                       </archive>
                   </configuration>
               </plugin>
               <plugin>
                   <artifactId>maven-assembly-plugin</artifactId>
                   <configuration>
                       <archive>
                           <manifest>
                               <mainClass>UpdateEvents</mainClass>
                           </manifest>
                       </archive>
                       <descriptorRefs>
                           <descriptorRef>jar-with-dependencies</descriptorRef>
                       </descriptorRefs>
                   </configuration>
               </plugin>
               <plugin>
                   <groupId>org.codehaus.mojo</groupId>
                   <artifactId>exec-maven-plugin</artifactId>
                   <version>1.2.1</version>
                   <executions>
                       <execution>
                           <phase>package</phase>
                           <goals>
                               <goal>java</goal>
                           </goals>
                       </execution>
                   </executions>
                   <configuration>
                       <mainClass>UpdateEvents</mainClass>
                       <cleanupDaemonThreads>false</cleanupDaemonThreads>
                   </configuration>
               </plugin>
           </plugins>
       </pluginManagement>
   </build>

</project>

And here’s the source code:

//Import Java Utilities
import java.io.IOException;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;

// Import Nylas Packages
import com.nylas.RequestFailedException;
import com.nylas.NylasAccount;
import com.nylas.NylasClient;
import com.nylas.Event;
import com.nylas.Participant;

//Import DotEnv to handle .env files
import io.github.cdimascio.dotenv.Dotenv;
import io.github.cdimascio.dotenv.DotenvException;

public class UpdateEvents {
   public static void main(String[] args) throws RequestFailedException, 
                                                 IOException {
       Dotenv dotenv = Dotenv.load();
       // Create the client object
       NylasClient client = new NylasClient();
       // Connect it to Nylas using the Access Token from the .env file
       NylasAccount account = client.account(dotenv.get("ACCESS_TOKEN"));

       // Get an event using it's Id
       Event event = account.events().get("{EVENT_ID}");

       // Define a placeholder for the new date/time
       Event.When when = null;
       // Get today's date
       LocalDate today = LocalDate.now();
       // Set time. As we're using UTC we need to 
       // add the hours in difference
       // from our own Timezone
       Instant sixPmUtc = today.atTime(14, 0).toInstant(ZoneOffset.UTC);
       // Set the date and time for the event. 
       // We add 30 minutes to the starting time
       when = new Event.Timespan(sixPmUtc, sixPmUtc.plus(30, 
                                 ChronoUnit.MINUTES));

       // Specify the date inside the event
       event.setWhen(when);

       // Update the event with the new values 
       // and notify all participants
       Event event_updated = account.events().update(event, true);
       if(event_updated.getId() != null){
           System.out.println("Event updated successfully");
       }else{
           System.out.println("There was an error updating the event");
       }
   }
}

To run our project, we need to compile it first. To do this,  we can open a Terminal window, go to our project’s root folder and type the following maven command:

$ mvn package

Once it’s done, we can run this command to execute our application:

$ mvn exec:java -Dexec.mainClass="UpdateEvents"
Updating Calendar Events

And we can confirm that the change was made:

Event updated

As we’ve seen here, updating an event is very similar to creating one. The Nylas Java SDK is flexible enough to make working with events simple and straightforward.

Deleting calendar events

Just for the sake of completion and taking advantage of the fact that we have the event id, let’s create a project to delete calendar events using Java. We’ll call this project Delete_Events, and the main class will be called DeleteEvents.

Here’s the pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <groupId>Nylas</groupId>
   <artifactId>Delete_Events</artifactId>
   <version>1.0-SNAPSHOT</version>

   <properties>
       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
       <maven.compiler.source>17</maven.compiler.source>
       <maven.compiler.target>17</maven.compiler.target>
   </properties>

   <dependencies>
       <dependency>
           <groupId>com.nylas.sdk</groupId>
           <artifactId>nylas-java-sdk</artifactId>
           <version>1.16.0</version>
       </dependency>
       <dependency>
           <groupId>org.slf4j</groupId>
           <artifactId>slf4j-jdk14</artifactId>
           <version>1.7.25</version>
       </dependency>
       <dependency>
           <groupId>io.github.cdimascio</groupId>
           <artifactId>dotenv-java</artifactId>
           <version>2.2.4</version>
       </dependency>
   </dependencies>

   <build>
       <pluginManagement>
           <plugins>
               <plugin>
                   <!-- Build an executable JAR -->
                   <groupId>org.apache.maven.plugins</groupId>
                   <artifactId>maven-jar-plugin</artifactId>
                   <version>3.1.0</version>
                   <configuration>
                       <archive>
                           <manifest>
                               <addClasspath>true</addClasspath>
                               <mainClass>DeleteEvents</mainClass>
                           </manifest>
                       </archive>
                   </configuration>
               </plugin>
               <plugin>
                   <artifactId>maven-assembly-plugin</artifactId>
                   <configuration>
                       <archive>
                           <manifest>
                               <mainClass>DeleteEvents</mainClass>
                           </manifest>
                       </archive>
                       <descriptorRefs>
                           <descriptorRef>jar-with-dependencies</descriptorRef>
                       </descriptorRefs>
                   </configuration>
               </plugin>
               <plugin>
                   <groupId>org.codehaus.mojo</groupId>
                   <artifactId>exec-maven-plugin</artifactId>
                   <version>1.2.1</version>
                   <executions>
                       <execution>
                           <phase>package</phase>
                           <goals>
                               <goal>java</goal>
                           </goals>
                       </execution>
                   </executions>
                   <configuration>
                       <mainClass>DeleteEvents</mainClass>
                       <cleanupDaemonThreads>false</cleanupDaemonThreads>
                   </configuration>
               </plugin>
           </plugins>
       </pluginManagement>
   </build>

</project>

And here’s the source code:

//Import Java Utilities
import java.io.IOException;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

// Import Nylas Packages
import com.nylas.RequestFailedException;
import com.nylas.NylasAccount;
import com.nylas.NylasClient;
import com.nylas.Event;
import com.nylas.Participant;

//Import DotEnv to handle .env files
import io.github.cdimascio.dotenv.Dotenv;
import io.github.cdimascio.dotenv.DotenvException;

public class DeleteEvents {
   public static void main(String[] args) throws RequestFailedException, 
                                                 IOException, 
                                                 InterruptedException {
       Dotenv dotenv = Dotenv.load();
       // Create the client object
       NylasClient client = new NylasClient();
       // Connect it to Nylas using the Access Token from the .env file
       NylasAccount account = client.account(dotenv.get("ACCESS_TOKEN"));
       String eventId = "cxlk9zo2g6shtnenpsfq7n41d";
       // Delete an event by specifying its ID and notify participants
       try{
           account.events().delete(eventId, true);
       } catch (Exception e){
           System.out.println("The event was already deleted");
       }
       // Get an event using its Id. 
       // If we cannot find it, it has been deleted
       Event event = account.events().get(eventId);
       if (event.getId() != null){
           System.out.println("The event was successfully deleted");
       }else{
           System.out.println("There was a problem deleting the event");
       }
   }
}

To run our project, we need to compile it first. To do this,  we can open a Terminal window, go to our project’s root folder and type the following maven command:

$ mvn package

Once it’s done, we can run this command to execute our application:

$ mvn exec:java -Dexec.mainClass="DeleteEvents"
Deleting Calendar Events

And that’s it! As you can see, by using the Nylas Java SDK, working with calendars becomes an easy task. If you want to learn more, visit our Documentation Page.

Related resources

How to create an appointment scheduler in your React app

Learn how to create an appointment scheduler in your React app using Nylas Scheduler. Streamline bookings and UX with step-by-step guidance.

Beyond APIs: Designing elegant, smart Web Components

Dive into the world of smart Web Components for advanced API integration solutions. Design elegant, customizable elements to elevate user experiences.

How to integrate a React calendar component to streamline scheduling

Elevate your app’s scheduling with a React calendar component and seamlessly integrate with Nylas for efficient calendar management.