Java

 

 

Introduction

In this tutorial, we will be building a simple calendar application using the Java programming language. The app will allow users to add and view events for specific dates in a calendar. This is a beginner-friendly project aimed at helping you understand how to work with Java’s LocalDate and Scanner classes.

Objective

The objective of this program is to create a basic calendar system where users can:

  • Add events to specific dates
  • View events scheduled for a specific date

Java Code for Calendar App

Below is the complete Java code for our simple Calendar App.

import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class CalendarApp {
    private Map<LocalDate, String> events;

    public CalendarApp() {
        events = new HashMap<>();
    }

    public void addEvent(LocalDate date, String eventDescription) {
        events.put(date, eventDescription);
    }

    public void viewEvent(LocalDate date) {
        if (events.containsKey(date)) {
            System.out.println("Event on " + date + ": " + events.get(date));
        } else {
            System.out.println("No events scheduled for this date.");
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        CalendarApp calendar = new CalendarApp();

        while (true) {
            System.out.println("\n--- Calendar App ---");
            System.out.println("1. Add Event");
            System.out.println("2. View Event");
            System.out.println("3. Exit");
            System.out.print("Enter your choice: ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // Consume newline

            switch (choice) {
                case 1:
                    System.out.print("Enter event date (YYYY-MM-DD): ");
                    String dateInput = scanner.nextLine();
                    LocalDate eventDate = LocalDate.parse(dateInput);
                    System.out.print("Enter event description: ");
                    String description = scanner.nextLine();
                    calendar.addEvent(eventDate, description);
                    System.out.println("Event added successfully.");
                    break;

                case 2:
                    System.out.print("Enter date to view events (YYYY-MM-DD): ");
                    String viewDateInput = scanner.nextLine();
                    LocalDate viewDate = LocalDate.parse(viewDateInput);
                    calendar.viewEvent(viewDate);
                    break;

                case 3:
                    System.out.println("Exiting the Calendar App. Goodbye!");
                    scanner.close();
                    System.exit(0);
                    break;

                default:
                    System.out.println("Invalid choice, please try again.");
            }
        }
    }
}

Explanation of the Program Structure

This program uses the following components:

  • Map<LocalDate, String>: A map to store events where the key is the event’s date and the value is the event description.
  • Scanner: This is used to read user input from the console.
  • LocalDate: Part of the java.time package, used for working with dates. We use it to handle the date input and manipulation.

How to Run the Program

  1. Ensure that you have Java installed on your system.
  2. Create a new file named CalendarApp.java and paste the provided Java code into the file.
  3. Open a terminal or command prompt and navigate to the folder where your CalendarApp.java file is located.
  4. Compile the Java program by running the following command:
    javac CalendarApp.java
  5. Run the compiled program by executing:
    java CalendarApp
  6. The program will display a simple text-based menu where you can add events and view events by date.
© 2025 Learn Programming. All Rights Reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)