Java GregorianCalendar Class



Introduction

The Java GregorianCalendar class is a concrete subclass of Calendar and provides the standard calendar system used by most of the world.Following are the important points about GregorianCalendar −

  • It is a hybrid calendar that supports both the Julian and Gregorian calendar systems with the support of a single discontinuity, which corresponds by default to the Gregorian date when the Gregorian calendar was instituted.

  • The Julian calendar specifies leap years every four years, whereas the Gregorian calendar omits century years which are not divisible by 400.

Class declaration

Following is the declaration for java.util.GregorianCalendar class −

public class GregorianCalendar
   extends Calendar

Field

Following are the fields for java.util.GregorianCalendar class −

  • static int AD − This is the value of the ERA field indicating the common era (Anno Domini), also known as CE.

  • static int BC − This is the value of the ERA field indicating the period before the common era (before Christ), also known as BCE.

Class constructors

Sr.No. Constructor & Description
1

GregorianCalendar()

This constructs a default GregorianCalendar using the current time in the default time zone with the default locale.

2

GregorianCalendar(int year, int month, int dayOfMonth)

This constructs a GregorianCalendar with the given date set in the default time zone with the default locale.

3

GregorianCalendar(int year, int month, int dayOfMonth, int hourOfDay, int minute)

This constructs a GregorianCalendar with the given date and time set for the default time zone with the default locale.

4

GregorianCalendar(int year, int month, int dayOfMonth, int hourOfDay, int minute, int second)

This constructs a GregorianCalendar with the given date and time set for the default time zone with the default locale.

5

GregorianCalendar(Locale aLocale)

This constructs a GregorianCalendar based on the current time in the default time zone with the given locale.

6

GregorianCalendar(TimeZone zone)

This constructs a GregorianCalendar based on the current time in the given time zone with the default locale.

7

GregorianCalendar(TimeZone zone, Locale aLocale)

This constructs a GregorianCalendar based on the current time in the given time zone with the given locale.

Class methods

Sr.No. Method & Description
1 void add(int field, int amount)

This method adds the specified (signed) amount of time to the given calendar field, based on the calendar's rules.

2 boolean equals(Object obj)

This method compares this GregorianCalendar to the specified Object.

3 static GregorianCalendar from​(ZonedDateTime zdt)

This method obtains an instance of GregorianCalendar with the default locale from a ZonedDateTime object.

4 int getActualMaximum(int field)

This method returns the maximum value that this calendar field could have, taking into consideration the given time value and the current values of the getFirstDayOfWeek, getMinimalDaysInFirstWeek, getGregorianChange and getTimeZone methods.

5 int getActualMinimum(int field)

This method returns the minimum value that this calendar field could have, taking into consideration the given time value and the current values of the getFirstDayOfWeek, getMinimalDaysInFirstWeek, getGregorianChange and getTimeZone methods.

6 String getCalendarType()

This method returns "gregory" as the calendar type.

7 int getGreatestMinimum(int field)

This method returns the highest minimum value for the given calendar field of this GregorianCalendar instance.

8 Date getGregorianChange()

This method gets the Gregorian Calendar change date.

9 int getLeastMaximum(int field)

This method returns the lowest maximum value for the given calendar field of this GregorianCalendar instance.

10 int getMaximum(int field)

This method returns the maximum value for the given calendar field of this GregorianCalendar instance.

11 int getMinimum(int field)

This method returns the minimum value for the given calendar field of this GregorianCalendar instance.

12 int getWeeksInWeekYear()

This method returns the number of weeks in the week year represented by this GregorianCalendar.

13 int getWeekYear()

This method returns the week year represented by this GregorianCalendar.

14 int hashCode()

This method generates the hash code for this GregorianCalendar object.

15 boolean isLeapYear(int year)

This method determines if the given year is a leap year.

16 boolean isWeekDateSupported()

This method returns true indicating this GregorianCalendar supports week dates.

17 void roll(int field, boolean up)

This method adds or subtracts (up/down) a single unit of time on the given time field without changing larger fields.

18 void setGregorianChange(Date date)

This method sets the GregorianCalendar change date.

19 void setWeekDate​(int weekYear, int weekOfYear, int dayOfWeek)

This method sets this GregorianCalendar to the date given by the date specifiers - weekYear, weekOfYear, and dayOfWeek.

20 ZonedDateTime toZonedDateTime()

This method converts this object to a ZonedDateTime that represents the same point on the time-line as this GregorianCalendar.

Methods inherited

This class inherits methods from the following classes −

  • java.util.Calendar
  • java.util.Object

Adding Two Months to Current Date of GregorianCalendar Instance Example

The following example shows the usage of Java GregorianCalendar add() method. We're creating a GregorianCalendar instance of current date and adding 2 months to it using add method and then printing the updated date.

package com.tutorialspoint;

import java.util.GregorianCalendar;

public class GregorianCalendarDemo {
   public static void main(String[] args) {

      // create a new calendar
      GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();

      // print the current date and time
      System.out.println("" + cal.getTime());

      // add 2 months 
      cal.add((GregorianCalendar.MONTH), 2);

      // print the modified date and time
      System.out.println("" + cal.getTime());
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Fri Nov 18 10:14:39 IST 2022
Wed Jan 18 10:14:39 IST 2023
Advertisements