Introduction
The Gregorian Calendar class is a subclass of the Calendar class in Java. It has many constructors and methods in Java. It has its own rules to interpret time information.
Constructors of Gregorian Calendar Class
- Gregorian Calendar( )
A default Gregorian Calendar is constructed by this constructor using the current time in the default time zone with the default locale.
- Gregorian Calendar(int year, int month, int date)
A Gregorian Calendar is constructed by this constructor with the given date set in the default time zone with the default locale.
- Gregorian Calendar(Time Zone zone)
A Gregorian Calendar is constructed by this constructor based on the current time in the given time zone with the default locale.
- Gregorian Calendar(Time Zone zone, Locale aLocale)
A Gregorian Calendar is constructed by this constructor based on the current time in the given time zone with the given locale.
- Gregorian Calendar(Locale aLocale)
A Gregorian Calendar is constructed by this constructor based on the current time in the given time zone with the given locale.
- Gregorian Calendar(int year, int month, int date, int hour, int minute)
A Gregorian Calendar is constructed by this constructor with the given date and time set for the default time zone with the default locale.
- Gregorian Calendar(int year, int month, int date, int hour, int minute, int second)
A Gregorian Calendar is constructed by this constructor with the given date and time set for the default time zone with the default locale.
Methods of Gregorian Calendar Class
- void add(int field, int amount)
The specified amount of time is added by this method to the given time field.
- protected void computeFields( )
The UTC is converted as milliseconds to the time field values by this method.
- Protected void computeTime( )
The Calendar is overriden by this method. Time field values are also converted to UTC as milliseconds.
- boolean equals(Object obj)
The Gregorian Calendar is compared to an object reference by this method.
- int get(int field)
It get the value for a given time field.
- int getActualMaximum(int field)
A maximum value is returned by this method that this field could have, given the current date.
- int getActualMinimum(int field)
A minimum value is returned by this method that this field could have, given the current date.
- int getGreatestMinimum(int field)
A highest minimum value is returned for the given field, if varies.
- Date getGregorianChange()
It gets the Gregorian Calendar change date.
- int getLeastMaximum(int field)
A lowest maximum value is returned by this method for the given field, if varies.
- int getMaximum(int field)
A maximum value is returned by this method for the given field.
- Date getTime( )
It gets the Calendar's current time.
- long getTimeInMillis( )
It gets the Calendar's current time as a long.
- Time Zone getTimeZone( )
It gets the time zone.
- int getMinimum(int field)
A minimum value is returned by this method for the given field.
- int hashCode( )
The Hash code is overriden by this method.
- boolean isLeapYear(int year)
It determines whether the given year is a leap year or not.
- void roll(int field, boolean up)
A single unit of time is added or subtracted by this method on the given time field without changing larger fields.
- void set(int field, int value)
The time field is set with the given value by this method.
- void set(int year, int month, int date)
The values for the fields year, month and date are set by this method.
- void set(int year, int month, int date, int hour, int minute)
The values for the fields year, month, date, hour and minute are set by this method.
- void set(int year, int month, int date, int hour, int minute, int second)
The values for the fields year, month, date, hour, minute and second are set by this method.
- void setGregorianChange(Date date)
The Gregorian Calendar change date is set by this method.
- void setTimeInMillis(long millis)
The Calendar's current time is set by this method from the given long value.
- void setTimeZone(TimeZone value)
The time zone is set by this method with the given time zone value.
- String toString( )
A string representation of the Calendar is returned by this method.
- void setTime(Date date)
The Calendar's current time is set by this method with the given date.
Example of Gregorian Calendar Class
package demo;
import java.util.*;
public class Demo
{
public static void main(String args[])
{
String months[] = { "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL",
"AUG", "SEP", "OCT", "NOV", "DEC" };
int year;
GregorianCalendar Greg_Clndr = new GregorianCalendar();
System.out.print("DATE: ");
System.out.print(months[Greg_Clndr.get(Calendar.MONTH)]);
System.out.print(" " + Greg_Clndr.get(Calendar.DATE) + " ");
System.out.println(year = Greg_Clndr.get(Calendar.YEAR));
System.out.print("TIME: ");
System.out.print(Greg_Clndr.get(Calendar.HOUR) + ":");
System.out.print(Greg_Clndr.get(Calendar.MINUTE) + ":");
System.out.println(Greg_Clndr.get(Calendar.SECOND));
if (Greg_Clndr.isLeapYear(year))
{
System.out.println("LEAP YEAR");
}
else
{
System.out.println("NOT A LEAP YEAR");
}
}
}
Output
Summary
This article explains the Gregorian Calendar class in Java.