Java GregorianCalendar Methods

java.util.GregorianCalendar is a concrete implementation of the abstract class java.util.Calendar. It inherits and implements methods from Calendar class:

There are seven overloaded constructors available to initialize the GregorianCalendar, besides get instance from Calendar class which for most Locale will produce a GregorianCalendar instance. Check Java Calendar and GregorianCalendar Examples. Let's refresh our memory with following example:

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

public class GregorianCalendarRevisitedExample {
    
    public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
        
        // create a calendar and set
        GregorianCalendar cal = new GregorianCalendar(1997, 4, 7, 10, 40, 30);

        // Displaying date using getTime() 
        System.out.println("Date           : " + sdf.format(cal.getTime()));
        
        // Print Calendar's field
        System.out.println("Year           : " + cal.get(Calendar.YEAR));
        System.out.println("Month          : " + cal.get(Calendar.MONTH));
        System.out.println("Date           : " + cal.get(Calendar.DATE));
        System.out.println("Hour of the Day: " + cal.get(Calendar.HOUR_OF_DAY));
        System.out.println("Minute         : " + cal.get(Calendar.MINUTE));
        System.out.println("Second         : " + cal.get(Calendar.SECOND));
        System.out.println();
        
        // Manipulating dates
        GregorianCalendar clonedCal = (GregorianCalendar) cal.clone();
        clonedCal.add(Calendar.DAY_OF_YEAR, 50);
        System.out.println("50 days later is: " + sdf.format(clonedCal.getTime()));
        
        cal.add(Calendar.MONTH, -18);
        System.out.println("18 months ago is: " + sdf.format(cal.getTime()));
        System.out.println();
        
        // increasing date, true indicates add 
        cal.roll(Calendar.DAY_OF_MONTH, true);
        // Displaying the result after operation 
        System.out.println("Roll Date: " + sdf.format(cal.getTime()));

        // Decrementing the date, false indicates subtraction 
        cal.roll(Calendar.DAY_OF_MONTH, false);
        System.out.println("Roll Date: " + sdf.format(cal.getTime()));
        
        // increasing month, roll +20 
        cal.roll(Calendar.MONTH, 20);
        System.out.println("Roll Date: " + sdf.format(cal.getTime()));
        
        // decreasing date, roll -30
        cal.roll(Calendar.DATE, -30);
        System.out.println("Roll Date: " + sdf.format(cal.getTime()));
        System.out.println();
        
        cal.setWeekDate(2018, 20, 5);
        System.out.println("Date: " + sdf.format(cal.getTime()));
    }
}
                    

Date           : 07-05-1997 10:40:30
Year           : 1997
Month          : 4
Date           : 7
Hour of the Day: 10
Minute         : 40
Second         : 30

50 days later is: 26-06-1997 10:40:30
18 months ago is: 07-11-1995 10:40:30

Roll Date: 08-11-1995 10:40:30
Roll Date: 07-11-1995 10:40:30
Roll Date: 07-07-1995 10:40:30
Roll Date: 08-07-1995 10:40:30

Date: 17-05-2018 10:40:30

Gregorian Change and Leap Year

The calendar that we use today, called Gregorian calendar, came into effect in October 15, 1582 in some countries and later in other countries. It replaces the Julian calendar. 10 days were removed from the calendar, i.e., October 4, 1582 (Julian) was followed by October 15, 1582 (Gregorian). The only difference between the Gregorian and the Julian calendar is the leap year rule. The Julian calendar specifies leap years every four years, whereas the Gregorian calendar omits century years which are not divisible by 400. Julian calendar also considers the first day of the year as march 25th, instead of January 1st. To change the cutover date, use setGregorianChange(...) method

GregorianCalendarChangeExample.java
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;

public class GregorianCalendarChangeExample {
    static SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");

    static void checkLeapYear(GregorianCalendar cal) {        
        // get Gregorian change and print it
        System.out.println("Gregorian Change  : " + sdf.format(cal.getGregorianChange()));
        System.out.println("2100 is Leap Year?: " + cal.isLeapYear(2100));
        System.out.println("2020 is Leap Year?: " + cal.isLeapYear(2020));
        System.out.println("2000 is Leap Year?: " + cal.isLeapYear(2000));
        System.out.println("1700 is Leap Year?: " + cal.isLeapYear(1700));
        System.out.println("1600 is Leap Year?: " + cal.isLeapYear(1600));
        System.out.println("1584 is Leap Year?: " + cal.isLeapYear(1584));
        System.out.println("1580 is Leap Year?: " + cal.isLeapYear(1580));
        System.out.println("1300 is Leap Year?: " + cal.isLeapYear(1300));
        System.out.println("1200 is Leap Year?: " + cal.isLeapYear(1200));
        System.out.println("1160 is Leap Year?: " + cal.isLeapYear(1160));
        System.out.println();
    }
    
    public static void main(String[] args) {
        // create a new GregorianCalendar
        GregorianCalendar cal1 = new GregorianCalendar();

        // print the current date and time
        System.out.println("Calendar Date     : " + sdf.format(cal1.getTime()));
        System.out.println();
        
        checkLeapYear(cal1);
        
        // create another GregorianCalendar
        GregorianCalendar cal2 = new GregorianCalendar();
        cal2.set(1199, 11, 31);
        cal2.setGregorianChange(cal2.getTime());
        checkLeapYear(cal2);
    }
}
                    

Calendar Date     : 23-07-2019 01:00:47

Gregorian Change  : 15-10-1582 08:00:00
2100 is Leap Year?: false
2020 is Leap Year?: true
2000 is Leap Year?: true
1700 is Leap Year?: false
1600 is Leap Year?: true
1584 is Leap Year?: true
1580 is Leap Year?: true
1300 is Leap Year?: true
1200 is Leap Year?: true
1160 is Leap Year?: true

Gregorian Change  : 31-12-1199 01:00:47
2100 is Leap Year?: false
2020 is Leap Year?: true
2000 is Leap Year?: true
1700 is Leap Year?: false
1600 is Leap Year?: true
1584 is Leap Year?: true
1580 is Leap Year?: true
1300 is Leap Year?: false
1200 is Leap Year?: true
1160 is Leap Year?: true

GregorianCalendar and ZonedDateTime

  • static GregorianCalendar from​(ZonedDateTime zdt): Obtains an instance of GregorianCalendar with the default locale from a ZonedDateTime object.
  • ZonedDateTime toZonedDateTime(): Converts this object to a ZonedDateTime that represents the same point on the time-line as this GregorianCalendar.
GregorianCalendarZonedDateTimeExample.java
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.GregorianCalendar;

public class GregorianCalendarZonedDateTimeExample {
    
    public static void main(String[] args) {       
        GregorianCalendar cal1 = new GregorianCalendar(2019, 6, 22, 8, 15, 45);
        ZonedDateTime d = cal1.toZonedDateTime();
        System.out.println(d);
        
        ZonedDateTime zdt =  ZonedDateTime.of​(1980, 4, 9, 0, 0, 0, 0, ZoneId.of​("Asia/Singapore"));
        GregorianCalendar cal2 = GregorianCalendar.from(zdt);
        System.out.println(cal2.getTime());
    }
}
                    

2019-07-22T08:15:45+08:00[Asia/Singapore]
Wed Apr 09 00:00:00 SGT 1980

Methods of GregorianCalendar

Following methods also available in GregorianCalendar:

  • int getActualMaximum(int field): 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.
  • int getActualMinimum(int field)​: 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.
  • String getCalendarType(): Returns "gregory" as the calendar type.
  • int getGreatestMinimum(int field): Returns the highest minimum value for the given calendar field of this GregorianCalendar instance.
  • int getLeastMaximum(int field): Returns the lowest maximum value for the given calendar field of this GregorianCalendar instance.
  • int getMaximum(int field): Returns the maximum value for the given calendar field of this GregorianCalendar instance.
  • int getMinimum(int field): Returns the minimum value for the given calendar field of this GregorianCalendar instance.
  • int getWeeksInWeekYear(): Returns the number of weeks in the week year represented by this GregorianCalendar.
  • int getWeekYear(): Returns the week year represented by this GregorianCalendar.
  • boolean isWeekDateSupported(): Returns true indicating this GregorianCalendar supports week dates.
GregorianCalendarMaxMinExample.java
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class GregorianCalendarMaxMinExample {
    
    public static void main(String[] args) {       
        SimpleDateFormat sdf = new SimpleDateFormat("EE MMM dd HH:mm:ss zzz yyyy");
        
        // create a cal
        GregorianCalendar cal = new GregorianCalendar();;
        System.out.printf("Calendar Date/Time             : %s\n", sdf.format(cal.getTime()));
        System.out.printf("Calendar Type                  : %s\n", cal.getCalendarType());
        System.out.printf("Weeks in Week Year             : %s\n", cal.getWeeksInWeekYear());
        System.out.printf("Week Year                      : %s\n", cal.getWeekYear());
        System.out.println();
        
        System.out.printf("Maximum of YEAR                : %s\n", cal.getMaximum(Calendar.YEAR));
        System.out.printf("Maximum of MONTH               : %s\n", cal.getMaximum(Calendar.MONTH));
        System.out.printf("Maximum of DATE                : %s\n", cal.getMaximum(Calendar.DATE));
        System.out.printf("Maximum of DAY_OF_YEAR         : %s\n", cal.getMaximum(Calendar.DAY_OF_YEAR));
        System.out.printf("Maximum of HOUR_OF_DAY         : %s\n", cal.getMaximum(Calendar.HOUR_OF_DAY));
        System.out.printf("Maximum of HOUR                : %s\n", cal.getMaximum(Calendar.HOUR));
        System.out.printf("Maximum of MINUTE              : %s\n", cal.getMaximum(Calendar.MINUTE));
        System.out.printf("Maximum of SECOND              : %s\n", cal.getMaximum(Calendar.SECOND));
        System.out.printf("Maximum of MILLISECOND         : %s\n", cal.getMaximum(Calendar.MILLISECOND));
        System.out.println();
        
        System.out.printf("Actual Maximum of YEAR         : %s\n", cal.getActualMaximum(Calendar.YEAR));
        System.out.printf("Actual Maximum of MONTH        : %s\n", cal.getActualMaximum(Calendar.MONTH));
        System.out.printf("Actual Maximum of DATE         : %s\n", cal.getActualMaximum(Calendar.DATE));
        System.out.printf("Actual Maximum of DAY_OF_YEAR  : %s\n", cal.getActualMaximum(Calendar.DAY_OF_YEAR));
        System.out.printf("Actual Maximum of HOUR_OF_DAY  : %s\n", cal.getActualMaximum(Calendar.HOUR_OF_DAY));
        System.out.printf("Actual Maximum of HOUR         : %s\n", cal.getActualMaximum(Calendar.HOUR));
        System.out.printf("Actual Maximum of MINUTE       : %s\n", cal.getActualMaximum(Calendar.MINUTE));
        System.out.printf("Actual Maximum of SECOND       : %s\n", cal.getActualMaximum(Calendar.SECOND));
        System.out.printf("Actual Maximum of MILLISECOND  : %s\n", cal.getActualMaximum(Calendar.MILLISECOND));
        System.out.println();
        
        System.out.printf("Least Maximum of YEAR          : %s\n", cal.getLeastMaximum(Calendar.YEAR));
        System.out.printf("Least Maximum of MONTH         : %s\n", cal.getLeastMaximum(Calendar.MONTH));
        System.out.printf("Least Maximum of DATE          : %s\n", cal.getLeastMaximum(Calendar.DATE));
        System.out.printf("Least Maximum of DAY_OF_YEAR   : %s\n", cal.getLeastMaximum(Calendar.DAY_OF_YEAR));
        System.out.printf("Least Maximum of HOUR_OF_DAY   : %s\n", cal.getLeastMaximum(Calendar.HOUR_OF_DAY));
        System.out.printf("Least Maximum of HOUR          : %s\n", cal.getLeastMaximum(Calendar.HOUR));
        System.out.printf("Least Maximum of MINUTE        : %s\n", cal.getLeastMaximum(Calendar.MINUTE));
        System.out.printf("Least Maximum of SECOND        : %s\n", cal.getLeastMaximum(Calendar.SECOND));
        System.out.printf("Least Maximum of MILLISECOND   : %s\n", cal.getLeastMaximum(Calendar.MILLISECOND));
        System.out.println();
        
        System.out.printf("Minimum of YEAR                : %s\n", cal.getMinimum(Calendar.YEAR));
        System.out.printf("Minimum of MONTH               : %s\n", cal.getMinimum(Calendar.MONTH));
        System.out.printf("Minimum of DATE                : %s\n", cal.getMinimum(Calendar.DATE));
        System.out.printf("Minimum of DAY_OF_YEAR         : %s\n", cal.getMinimum(Calendar.DAY_OF_YEAR));
        System.out.printf("Minimum of HOUR_OF_DAY         : %s\n", cal.getMinimum(Calendar.HOUR_OF_DAY));
        System.out.printf("Minimum of HOUR                : %s\n", cal.getMinimum(Calendar.HOUR));
        System.out.printf("Minimum of MINUTE              : %s\n", cal.getMinimum(Calendar.MINUTE));
        System.out.printf("Minimum of SECOND              : %s\n", cal.getMinimum(Calendar.SECOND));
        System.out.printf("Minimum of MILLISECOND         : %s\n", cal.getMinimum(Calendar.MILLISECOND));
        System.out.println();
        
        System.out.printf("Actual Minimum of YEAR         : %s\n", cal.getActualMinimum(Calendar.YEAR));
        System.out.printf("Actual Minimum of MONTH        : %s\n", cal.getActualMinimum(Calendar.MONTH));
        System.out.printf("Actual Minimum of DATE         : %s\n", cal.getActualMinimum(Calendar.DATE));
        System.out.printf("Actual Minimum of DAY_OF_YEAR  : %s\n", cal.getActualMinimum(Calendar.DAY_OF_YEAR));
        System.out.printf("Actual Minimum of HOUR_OF_DAY  : %s\n", cal.getActualMinimum(Calendar.HOUR_OF_DAY));
        System.out.printf("Actual Minimum of HOUR         : %s\n", cal.getActualMinimum(Calendar.HOUR));
        System.out.printf("Actual Minimum of MINUTE       : %s\n", cal.getActualMinimum(Calendar.MINUTE));
        System.out.printf("Actual Minimum of SECOND       : %s\n", cal.getActualMinimum(Calendar.SECOND));
        System.out.printf("Actual Minimum of MILLISECOND  : %s\n", cal.getActualMinimum(Calendar.MILLISECOND));
        System.out.println();
        
        System.out.printf("Greatest Minimum of YEAR       : %s\n", cal.getGreatestMinimum(Calendar.YEAR));
        System.out.printf("Greatest Minimum of MONTH      : %s\n", cal.getGreatestMinimum(Calendar.MONTH));
        System.out.printf("Greatest Minimum of DATE       : %s\n", cal.getGreatestMinimum(Calendar.DATE));
        System.out.printf("Greatest Minimum of DAY_OF_YEAR: %s\n", cal.getGreatestMinimum(Calendar.DAY_OF_YEAR));
        System.out.printf("Greatest Minimum of HOUR_OF_DAY: %s\n", cal.getGreatestMinimum(Calendar.HOUR_OF_DAY));
        System.out.printf("Greatest Minimum of HOUR       : %s\n", cal.getGreatestMinimum(Calendar.HOUR));
        System.out.printf("Greatest Minimum of MINUTE     : %s\n", cal.getGreatestMinimum(Calendar.MINUTE));
        System.out.printf("Greatest Minimum of SECOND     : %s\n", cal.getGreatestMinimum(Calendar.SECOND));
        System.out.printf("Greatest Minimum of MILLISECOND: %s\n", cal.getGreatestMinimum(Calendar.MILLISECOND));
        System.out.println();
    }
}
                    

Calendar Date/Time             : Sat Jul 20 15:02:33 SGT 2019
Calendar Type                  : gregory
Weeks in Week Year             : 52
Week Year                      : 2019

Maximum of YEAR                : 292278994
Maximum of MONTH               : 11
Maximum of DATE                : 31
Maximum of DAY_OF_YEAR         : 366
Maximum of HOUR_OF_DAY         : 23
Maximum of HOUR                : 11
Maximum of MINUTE              : 59
Maximum of SECOND              : 59
Maximum of MILLISECOND         : 999

Actual Maximum of YEAR         : 292278994
Actual Maximum of MONTH        : 11
Actual Maximum of DATE         : 31
Actual Maximum of DAY_OF_YEAR  : 365
Actual Maximum of HOUR_OF_DAY  : 23
Actual Maximum of HOUR         : 11
Actual Maximum of MINUTE       : 59
Actual Maximum of SECOND       : 59
Actual Maximum of MILLISECOND  : 999

Least Maximum of YEAR          : 292269054
Least Maximum of MONTH         : 11
Least Maximum of DATE          : 28
Least Maximum of DAY_OF_YEAR   : 355
Least Maximum of HOUR_OF_DAY   : 23
Least Maximum of HOUR          : 11
Least Maximum of MINUTE        : 59
Least Maximum of SECOND        : 59
Least Maximum of MILLISECOND   : 999

Minimum of YEAR                : 1
Minimum of MONTH               : 0
Minimum of DATE                : 1
Minimum of DAY_OF_YEAR         : 1
Minimum of HOUR_OF_DAY         : 0
Minimum of HOUR                : 0
Minimum of MINUTE              : 0
Minimum of SECOND              : 0
Minimum of MILLISECOND         : 0

Actual Minimum of YEAR         : 1
Actual Minimum of MONTH        : 0
Actual Minimum of DATE         : 1
Actual Minimum of DAY_OF_YEAR  : 1
Actual Minimum of HOUR_OF_DAY  : 0
Actual Minimum of HOUR         : 0
Actual Minimum of MINUTE       : 0
Actual Minimum of SECOND       : 0
Actual Minimum of MILLISECOND  : 0

Greatest Minimum of YEAR       : 1
Greatest Minimum of MONTH      : 0
Greatest Minimum of DATE       : 1
Greatest Minimum of DAY_OF_YEAR: 1
Greatest Minimum of HOUR_OF_DAY: 0
Greatest Minimum of HOUR       : 0
Greatest Minimum of MINUTE     : 0
Greatest Minimum of SECOND     : 0
Greatest Minimum of MILLISECOND: 0

Conclusion

java.util.Calendar is an abstract class. Calendar.getInstance() returns an implementation class java.util.GregorianCalendar (except locales of "th" and "jp"). In Java, this GregorianCalendar handles both the Gregorian calendar as well as the Julian calendar, including the cut over.