java.util.Calendar

The java.util.Calendar class is an abstract class used to perform date and time arithmetic such as adding days, months, or retrieving specific fields (like year, month, day, etc.). It provides more functionality than java.util.Date and was introduced to overcome some limitations of the Date class.

Key Features:

  • Allows field-level manipulation (like setting specific year, month, day).
  • Supports internationalization and localization.
  • Can be used to convert between Date and Calendar.

Commonly Used Methods

Field Constants:
  • Calendar.YEAR
  • Calendar.MONTH (0-based: Jan = 0)
  • Calendar.DAY_OF_MONTH
  • Calendar.HOUR, Calendar.MINUTE, etc.

Simple Program

import java.util.Calendar;

public class SimpleCalendarExample {
    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();

        System.out.println("Current Date and Time:");
        System.out.println("Year: " + cal.get(Calendar.YEAR));
        System.out.println("Month: " + (cal.get(Calendar.MONTH) + 1)); // 0-based index
        System.out.println("Day: " + cal.get(Calendar.DAY_OF_MONTH));
        System.out.println("Hour: " + cal.get(Calendar.HOUR_OF_DAY));
        System.out.println("Minute: " + cal.get(Calendar.MINUTE));
    }
}
Current Date and Time:
Year: 2025
Month: 5
Day: 26
Hour: 16
Minute: 45Code language: JavaScript (javascript)

Problem Statement 

LotusJavaPrince  building a banking system that sends loan payment reminders exactly 15 days before the due date. They want to calculate and display this reminder date using java.util.Calendar, ensuring users are notified in advance.

import java.util.Calendar;
import java.text.SimpleDateFormat;

class Loan {
    private String customerName;
    private Calendar dueDate;

    public Loan(String customerName, int year, int month, int day) {
        this.customerName = customerName;
        this.dueDate = Calendar.getInstance();
        this.dueDate.set(year, month - 1, day); // Month is 0-based
    }

    public Calendar getReminderDate() {
        Calendar reminderDate = (Calendar) dueDate.clone();
        reminderDate.add(Calendar.DAY_OF_MONTH, -15);
        return reminderDate;
    }

    public void printReminder() {
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
        System.out.println("Loan reminder for " + customerName);
        System.out.println("Due Date: " + sdf.format(dueDate.getTime()));
        System.out.println("Reminder Date: " + sdf.format(getReminderDate().getTime()));
    }
}

public class LoanReminderSystem {
    public static void main(String[] args) {
        Loan maheshLoan = new Loan("Mahesh", 2025, 7, 10); // July 10, 2025
        Loan paaniLoan = new Loan("Paani", 2025, 8, 5); // August 5, 2025

        maheshLoan.printReminder();
        System.out.println();
        paaniLoan.printReminder();
    }
}
Loan reminder for Mahesh
Due Date: 10-07-2025
Reminder Date: 25-06-2025

Loan reminder for Paani
Due Date: 05-08-2025
Reminder Date: 21-07-2025Code language: JavaScript (javascript)

The java.util.Calendar class is a powerful tool for date and time manipulation, offering granular control over individual components like year, month, and day. Unlike the Date class, it supports arithmetic operations and is ideal for calculations like adding days, setting specific dates, and comparing times.

While Calendar is still in use, developers are encouraged to migrate to java.time APIs (LocalDate, ZonedDateTime, etc.) for better clarity, immutability, and thread safety in modern Java applications. However, a solid understanding of Calendar remains essential when maintaining or integrating with legacy systems.  

Scroll to Top