Java Calendar Class

The Calendar class in Java is an abstract class that provides methods for working with dates, times, and time zones. It is part of the java.util package and is used to manipulate date and time information in a calendar system.

The Calendar class provides several static methods for creating instances of the Calendar class based on various time zones and locales. The most common way to create a Calendar object is to use the getInstance() method, which returns a Calendar object based on the current time zone and locale.

Here’s an example of creating a Calendar object using the getInstance() method:

Calendar cal = Calendar.getInstance();

Once you have a Calendar object, you can use its methods to manipulate dates and times. Some common methods include:

  • get(int field): Returns the value of the specified field (such as YEAR, MONTH, DAY_OF_MONTH, etc.).
  • set(int field, int value): Sets the value of the specified field.
  • add(int field, int amount): Adds the specified amount to the specified field.
  • getTime(): Returns a Date object representing the time value of the Calendar object.
  • setTime(Date date): Sets the time value of the Calendar object to the specified Date.

Here’s an example of using some of these methods to manipulate a Calendar object:

// Set the calendar to January 1, 2023
cal.set(Calendar.YEAR, 2023);
cal.set(Calendar.MONTH, Calendar.JANUARY);
cal.set(Calendar.DAY_OF_MONTH, 1);

// Add 1 week to the calendar
cal.add(Calendar.WEEK_OF_YEAR, 1);

// Get the date as a Date object
Date date = cal.getTime();

Note that the Calendar class is often criticized for being overly complex and difficult to use. In Java 8 and later, it is recommended to use the java.time package instead, which provides a more modern and intuitive API for working with dates and times.

Java Calendar class declaration:

The Calendar class in Java is an abstract class, so you cannot create an instance of it directly. Instead, you create an instance of one of its concrete subclasses such as GregorianCalendar or JapaneseCalendar.

Here is the declaration of the Calendar class:

public abstract class Calendar extends Object implements Serializable, Cloneable, Comparable<Calendar> {
    // Class body
}

As you can see, the Calendar class is declared as abstract, which means that it cannot be instantiated directly. It provides some common methods for working with dates and times, but the specific behavior of these methods is implemented in its concrete subclasses.

The Calendar class extends the Object class and implements several interfaces, including Serializable, Cloneable, and Comparable<Calendar>. This means that Calendar objects can be serialized, cloned, and compared to other Calendar objects.

List of Calendar Methods:

The Calendar class in Java provides a wide range of methods for working with dates, times, and time zones. Here is a list of some of the most commonly used methods:

  • getInstance(): Returns a Calendar object initialized with the current date and time in the default time zone and locale.
  • get(int field): Returns the value of the specified field, such as YEAR, MONTH, DAY_OF_MONTH, HOUR_OF_DAY, MINUTE, SECOND, etc.
  • set(int field, int value): Sets the value of the specified field.
  • add(int field, int amount): Adds the specified amount to the specified field.
  • roll(int field, int amount): Adds the specified amount to the specified field without changing the larger fields.
  • getTime(): Returns a Date object representing the time value of the Calendar object.
  • setTime(Date date): Sets the time value of the Calendar object to the specified Date.
  • getActualMaximum(int field): Returns the maximum value that the specified field can have for the current date.
  • getActualMinimum(int field): Returns the minimum value that the specified field can have for the current date.
  • getFirstDayOfWeek(): Returns the first day of the week in the current locale.
  • setFirstDayOfWeek(int value): Sets the first day of the week in the current locale.
  • getTimeInMillis(): Returns the time value of the Calendar object in milliseconds.
  • setTimeInMillis(long millis): Sets the time value of the Calendar object using the specified time value in milliseconds.
  • getTimeZone(): Returns the time zone of the Calendar object.
  • setTimeZone(TimeZone timezone): Sets the time zone of the Calendar object to the specified TimeZone.
  • isLeapYear(int year): Returns true if the specified year is a leap year in the current calendar system.
  • before(Object when): Returns true if the time value of the Calendar object is before the time value of the specified object.
  • after(Object when): Returns true if the time value of the Calendar object is after the time value of the specified object.

These are just a few examples of the methods available in the Calendar class. There are many more methods that provide additional functionality for working with dates, times, and time zones.

Java Calendar Class Example: get()

The get(int field) method of the Calendar class in Java is used to retrieve the value of the specified field, such as the year, month, day of the month, hour, minute, and second. Here is an example that demonstrates how to use the get() method to retrieve the current year, month, and day of the month:

import java.util.Calendar;

public class CalendarExample {
    public static void main(String[] args) {
        // Create a Calendar object
        Calendar cal = Calendar.getInstance();

        // Get the current year, month, and day of the month
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH);
        int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);

        // Display the results
        System.out.println("Current Date: " + year + "-" + (month + 1) + "-" + dayOfMonth);
    }
}

In this example, we first create a Calendar object using the getInstance() method. Then we use the get() method to retrieve the current year, month, and day of the month by passing in the corresponding Calendar field constants (Calendar.YEAR, Calendar.MONTH, and Calendar.DAY_OF_MONTH, respectively).

Finally, we display the results by printing them to the console. Note that we add 1 to the month value since it is zero-based (January is 0, February is 1, etc.).

When you run this code, it will display the current date in the format yyyy-MM-dd.

Java Calendar Class Example: getInstance()

The getInstance() method of the Calendar class in Java is used to create a Calendar object initialized with the current date and time in the default time zone and locale. Here is an example that demonstrates how to use the getInstance() method:

import java.util.Calendar;

public class CalendarExample {
    public static void main(String[] args) {
        // Create a Calendar object with the current date and time
        Calendar cal = Calendar.getInstance();

        // Display the results
        System.out.println("Current Date and Time: " + cal.getTime());
    }
}

In this example, we use the getInstance() method to create a Calendar object that is initialized with the current date and time in the default time zone and locale.

Then, we call the getTime() method to retrieve a Date object representing the time value of the Calendar object. Finally, we display the current date and time by printing the Date object to the console.

When you run this code, it will display the current date and time in the default format. The output will look something like this:

Current Date and Time: Fri May 07 14:30:00 EDT 2023

Java Calendar Class Example: getMaximum()

The getActualMaximum(int field) method of the Calendar class in Java is used to retrieve the maximum value that the specified field can have for the current date. Here is an example that demonstrates how to use the getActualMaximum() method to retrieve the maximum number of days in the current month:

import java.util.Calendar;

public class CalendarExample {
    public static void main(String[] args) {
        // Create a Calendar object
        Calendar cal = Calendar.getInstance();

        // Get the maximum number of days in the current month
        int maxDays = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

        // Display the results
        System.out.println("Maximum number of days in the current month: " + maxDays);
    }
}

In this example, we first create a Calendar object using the getInstance() method. Then we use the getActualMaximum() method to retrieve the maximum number of days in the current month by passing in the Calendar.DAY_OF_MONTH field constant.

Finally, we display the maximum number of days in the current month by printing it to the console.

When you run this code, it will display the maximum number of days in the current month. For example, if the current month is May, the output will be:

Maximum number of days in the current month: 31

Java Calendar Class Example: getMinimum()

The getActualMinimum(int field) method of the Calendar class in Java is used to retrieve the minimum value that the specified field can have for the current date. Here is an example that demonstrates how to use the getActualMinimum() method to retrieve the minimum value of the day of the week:

import java.util.Calendar;

public class CalendarExample {
    public static void main(String[] args) {
        // Create a Calendar object
        Calendar cal = Calendar.getInstance();

        // Get the minimum value of the day of the week
        int minDay = cal.getActualMinimum(Calendar.DAY_OF_WEEK);

        // Display the results
        System.out.println("Minimum value of the day of the week: " + minDay);
    }
}

In this example, we first create a Calendar object using the getInstance() method. Then we use the getActualMinimum() method to retrieve the minimum value of the day of the week by passing in the Calendar.DAY_OF_WEEK field constant.

Finally, we display the minimum value of the day of the week by printing it to the console.

When you run this code, it will display the minimum value of the day of the week. In the Gregorian calendar system, which is used by the Calendar class, the minimum value of the day of the week is Calendar.SUNDAY, which has a value of 1. Therefore, the output of this example will be:

Minimum value of the day of the week: 1