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
- Ensure that you have Java installed on your system.
- Create a new file named
CalendarApp.java
and paste the provided Java code into the file. - Open a terminal or command prompt and navigate to the folder where your
CalendarApp.java
file is located. - Compile the Java program by running the following command:
javac CalendarApp.java
- Run the compiled program by executing:
java CalendarApp
- The program will display a simple text-based menu where you can add events and view events by date.