Java provides an abstract class called Calendar
. It defines a set of fields for storing date and time such as YEAR
, MONTH
, DAY_OF_MONTH
, HOUR
, and so on, as well as methods for date-time conversions and manipulating the calendar fields.
The Calendar
class also provides a set of calendar-based algorithms. With these, we can perform actions like determining the number of days between two dates and converting between time units, such as years and days.
The GregorianCalendar
class is a concrete subclass of the Calendar
class that represents a calendar with the Gregorian calendar system. It is used to manage dates and times.
The Gregorian calendar is the standard international calendar that was first introduced in 1582 by Pope Gregory XIII. It is used in most parts of the world today. It is a solar calendar with 12 months of 28-31 days each.
The Gregorian calendar is designed so that the average length of a year is 365.2425 days. The year in the Gregorian calendar is very close to the true length of a year in the tropical (solar) year. This accuracy is achieved by having 97 leap years every 400 years.
See the code snippet below to understand the key differences between the implementation of Calendar
class and GregorianCalendar
class in Java.
class GregorianCalendar extends Calendar{// Fieldspublic static final int AD;public static final int BC;// Constructorspublic GregorianCalendar() {// ...}// ...// Methodpublic void add(int field, int amount) {// ...}protected void computeFields() {// ...}protected void computeTime() {// ...}public TimeZone getTimeZone() {// ...}// ...}
GregorianCalendar
classThe GregorianCalendar
class provides a set of fields that identify particular aspects of the calendar. We can use these fields with the get()
and set()
methods to retrieve and modify calendar field values.
The following are the fields of GregorianCalendar
class:
YEAR
: This represents the year.
MONTH
: This represents the month. The value of the MONTH
field is 0-based. This means that 0 represents January, and 11 represents December.
DAY_OF_MONTH
: This represents the day of the month. It has values from 1 to 31, representing each day in a month.
HOUR_OF_DAY
: This represents the hour of the day. The value of the HOUR_OF_DAY
field is 0-based. This means that 0 represents midnight, and 23 represents 11 pm.
MINUTE
: This represents the minute within the hour.
SECOND
: This represents the second within the minute.
MILLISECOND
: This represents the millisecond within the second.
ZONE_OFFSET
: This represents the offset from GMT of the time zone used to compute this calendar in milliseconds.
DST_OFFSET
: This represents the offset from GMT+00:00 of the daylight saving time zone used to compute this calendar in milliseconds.
GregorianCalendar
classThe GregorianCalendar
class provides a set of methods for manipulating the calendar fields and for managing calendar-based events. Some of the commonly used methods are as follows:
getInstance()
: This returns a GregorianCalendar
object initialized with the current date and time in the default time zone with the default locale.
getGregorianChange()
: This returns the Gregorian calendar reform date as a Date
object.
isLeapYear(int year)
: This determines whether the given year is a leap year in the Gregorian calendar.
setTime(Date date)
: This sets the GregorianCalendar
to the time represented by the Date
object.
getTime()
: This returns a Date
object that represents the Gregorian calendar's time value.
add(int field, int amount)
: This adds the specified amount of time to the given calendar field, based on the calendar’s rules.
roll(int field, int amount)
: This adds the specified amount of time to the given calendar field, without changing larger fields.
setTimeZone(TimeZone zone)
: This sets the time zone with the given time zone value.
getTimeZone()
: This gets the time zone.
setLenient(boolean lenient)
: This specifies whether or not the calendar should be lenient.
isLenient()
: This checks if this calendar is lenient.
setFirstDayOfWeek(int value)
: This sets the first day of the week.
getFirstDayOfWeek()
: This gets the first day of the week.
setMinimalDaysInFirstWeek(int value)
: This sets the minimal number of days in the first week.
getMinimalDaysInFirstWeek()
: This gets the minimal number of days in the first week.
The following code shows how to use the GregorianCalendar
class.
import java.util.*;import java.time.DayOfWeek;public class TestGregorianCalendar {public static void main(String[] args) {// create a new calendarGregorianCalendar cal = new GregorianCalendar();// print the current date and timeSystem.out.println("The inputted date is: "+ cal.getTime());// print the current GregorianCalendar reform dateSystem.out.println("The Gregorian Calendar reform date is: "+ cal.getGregorianChange());// check for leap yearif (cal.isLeapYear(cal.get(Calendar.YEAR))) {System.out.println("It is a leap year.");} else {System.out.println("It is NOT a leap year.");}// create a new calendar for August 12, 2022cal = new GregorianCalendar(2022, 7, 12);// print the date using get() of the parent class CalenderSystem.out.println("Date: " + cal.get(Calendar.DATE));System.out.println("Month: " + cal.get(Calendar.MONTH));System.out.println("Year: " + cal.get(Calendar.YEAR));}}
GregorianCalendar
object and set it to the current date and time. We also print out the information about the current date and time and the Gregorian calendar reform date.