Java GregorianCalendar | Example Program

Last Updated on February 11, 2021 by Scientech Easy

GregorianCalendar in Java is a concrete subclass of an abstract class Calender. In other words, it is an implementation of a Calendar class in Java API. It is the most commonly used calendar.

A calendar in java is an abstract superclass for obtaining detailed calendar information such as year, month, date, hour, minute, and second.

Since Calendar class is an abstract class, we cannot instantiate it. That is, we cannot create an object of an abstract class. But we can create an object of GregorianCalendar because it is a concrete class.

GregorianCalendar was introduced in JDK 1.1. It is present in java.util.GregorianCalendar package.

GregorianCalendar class declaration


GregorianCalendar is a concrete class that can be declared as follows:

public class GregorianCalendar
    extends Calendar

Java GregorianCalendar class extends Calendar class and implements Serializable, Cloneable, and Comparable<Calendar> interfaces. Calendar class extends Object class.

GregorianCalendar Class Constructors in Java


Java GregorianCalendar class provides several constructors that are as follows:

1. GregorianCalendar(): This constructor creates a default GregorianCalendar using the current time in the default time zone with the default locale.

In other words, it initializes the object with the current date and time in the default locale and time zone.


The syntax to create an object of GregorianCalendar class for current data and time is as follows:

GregorianCalendar gcal = new GregorianCalendar();

2. GregorianCalendar(int year, int month, int dayOfMonth): This form of constructor creates a GregorianCalendar for the specified year, month, and date.

The general syntax to create an object of GregorianCalendar for the specified year, month, and date is as follows:

GregorianCalendar gcal = new GregorianCalendar(int year, int month, int dayOfMonth);
For example:
GregorianCalendar gcal = new GregorianCalendar(2021, 2, 10);

3. GregorianCalendar(int year, int month, int dayOfMonth, int hourOfDay, int minute): This form of constructor creates a GregorianCalendar for the specified year, month, date, hour, and minute.

The general syntax to create an object of GregorianCalendar for the specified year, month, date, hour, and minute is as follows:

GregorianCalendar gcal = new GregorianCalendar(int year, int month, int dayOfMonth, int hourOfDay, int minute);
For example:
GregorianCalendar gcal = new GregorianCalendar(2021, 2, 10, 09, 50);

4. GregorianCalendar(int year, int month, int dayOfMonth, int hourOfDay, int minute, int second): This constructor constructs a GregorianCalendar for the specified year, month, date, hour, minute, and second.

The general syntax to create an object of GregorianCalendar for the specified year, month, date, hour, minute, and second is as follows:

GregorianCalendar gcal = new GregorianCalendar(int year, int month, int dayOfMonth, int hourOfDay, int minute, int second);
For example:
GregorianCalendar gcal = new GregorianCalendar(2021, 2, 10, 09, 50, 25);

5. GregorianCalendar(Locale aLocale): It creates a GregorianCalendar based on the current time in the default time zone with the specified locale.

6. GregorianCalendar(TimeZone zone): This constructor constructs a GregorianCalendar based on the current time in the specified time zone and default locale.


7. GregorianCalendar(TimeZone zone, Locale aLocale): It creates a GregorianCalendar based on the current time in the specified time zone and locale.

GregorianCalendar Methods in Java


In addition to methods inherited from Calendar class and Object class, Java GregorianCalendar class also provide some useful methods. They are as follows:

1. void add(int field, int amount): This method is used to add the specified (signed) amount of time to the specified calendar field, based on the calendar’s rules.

2. Object clone(): This method creates and returns a copy of this object.

3. protected void computeFields(): This method converts the time value to calendar field values.

4. protected void computeTime(): It converts calendar field values to the time value.

5. boolean equals(Object obj): This method is used to compare the GregorianCalendar object to the specified Object obj.

6. int getActualMaximum(int field): This method is used to retrieve the maximum value that a specified calendar field can have.

7. int getActualMinimum(int field): This method is used to retrieve the minimum value that a specified calendar field can have.

8. int getGreatestMinimum(int field): It retrieves the highest minimum value for the specified calendar field of the current GregorianCalendar instance.

9. Date getGregorianChange(): It gets the Gregorian Calendar change date.

10. int getLeastMaximum(int field): This method returns the lowest maximum value for the specified calendar field of the current GregorianCalendar instance.

11. int getMaximum(int field): It returns the maximum value for the specified calendar field of the current GregorianCalendar instance.

12. int getMinimum(int field): It returns the minimum value for the specified calendar field of the current GregorianCalendar instance.

13. TimeZone getTimeZone(): It is used to get the time zone.

14. int getWeeksInWeekYear(): It returns the number of weeks in the week year represented by the GregorianCalendar.

15. int getWeekYear(): This method returns the week year represented by the GregorianCalendar.

16. int hashCode(): It generates the hash code value for the GregorianCalendar object.

17. boolean isLeapYear(int year): The isLeapYear() method is used to determine if the specified year is a leap year or not. It returns true if year is a leap year otherwise, returns false.

18. boolean isWeekDateSupported(): It returns true if the GregorianCalendar supports week dates.

19. void roll(int field, boolean up): It adds or subtracts (up/down) a single unit of time on the specified time field without changing larger fields.

20. void roll(int field, int amount): It adds a signed amount to the given calendar field without changing larger fields.

21. void setGregorianChange(Date date): It sets the GregorianCalendar change date.

22. void setTimeZone(TimeZone zone): It sets the time zone with the specified time zone value.

Note: Java GregorianCalendar class also inherits several constant fields and methods from the Calendar class. These contact fields are used as arguments to methods in the class.

Example Program based on GregorianCalendar in Java


1. Let’s take an example program where we will display the current date and time information of the system. We will also check that the current year is a leap year or not using isYearLeap() method provided by Java GregorianCalendar.

Program source code 1:

import java.util.Calendar;
import java.util.GregorianCalendar;
public final class GCalendarTest {
public static void main(String[] args) 
{
//  Create an array of type String to store month abbreviations.
String months[] = {
        "Jan", "Feb", "Mar", "Apr",
         "May", "Jun", "Jul", "Aug",
		 "Sep", "Oct", "Nov", "Dec"};
 int year;
// Create an object of Gregorian calendar initialized with the current date and time in the default locale and timezone.
   GregorianCalendar gcal = new GregorianCalendar();
			
// Display current date and time information.
   System.out.print("Date Info: ");
   System.out.print(months[gcal.get(Calendar.MONTH)]);
   System.out.print(" " + gcal.get(Calendar.DATE) + ", ");
   System.out.println(year = gcal.get(Calendar.YEAR));
			
  System.out.print("Time Info: ");
  System.out.print(gcal.get(Calendar.HOUR) + ":");
  System.out.print(gcal.get(Calendar.MINUTE) + ":");
  System.out.println(gcal.get(Calendar.SECOND));
			
// Test if the current year is a leap year or not.
   if(gcal.isLeapYear(year)) {
        System.out.println("Current year is a leap year");
	}
    else {
	    System.out.println("Current year is not a leap year");
    }  
   }
}
Output:
      Date: Feb 9, 2021
      Time: 0:43:14
      Current year is not a leap year

2. Let’s create a calendar for February 2021 and will determine the name of the day of month. For this, we will create an object of Gregorian calendar class and pass year, month, and day as arguments to its constructor.

Look at the program source code given below.

Program source code 2:

import java.util.Calendar;
import java.util.GregorianCalendar;
public final class Test {
public static void main(String[] args) 
{
//  Create an array of type String to store name of day.
String dayNameOfWeek[] = {
        "Sunday", "Monday", "Tuesday", "Wednesday",
         "Thursday", "Friday", "Saturday"};

// Create an object of Gregorian calendar initialized with February 9 2021.
   GregorianCalendar gcal = new GregorianCalendar(2021, 2, 9); // Passing year, month, dayOfMonth as parameters.
   
  System.out.println("February 09, 2021, is a " + dayNameOfWeek[gcal.get(Calendar.DAY_OF_WEEK) - 1]);
  }
}
Output:
      February 09, 2021, is a Tuesday

Hope that this tutorial has covered all the important points related Java GregorianCalendar with example programs. I hope that you will have understood this simple topic and enjoyed it.
Thanks for reading!!!