Gregorian Calendar Class With Date and Time in Java

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

  1. package demo;  
  2. import java.util.*;  
  3. public class Demo  
  4. {  
  5.     public static void main(String args[])  
  6.     {  
  7.         String months[] = { "JAN""FEB""MAR""APR""MAY""JUN""JUL",  
  8.                             "AUG""SEP""OCT""NOV""DEC" };  
  9.         int year;  
  10.         GregorianCalendar Greg_Clndr = new GregorianCalendar();  
  11.         System.out.print("DATE: ");  
  12.         System.out.print(months[Greg_Clndr.get(Calendar.MONTH)]);  
  13.         System.out.print(" " + Greg_Clndr.get(Calendar.DATE) + " ");  
  14.         System.out.println(year = Greg_Clndr.get(Calendar.YEAR));  
  15.         System.out.print("TIME: ");  
  16.         System.out.print(Greg_Clndr.get(Calendar.HOUR) + ":");  
  17.         System.out.print(Greg_Clndr.get(Calendar.MINUTE) + ":");  
  18.         System.out.println(Greg_Clndr.get(Calendar.SECOND));  
  19.         if (Greg_Clndr.isLeapYear(year))  
  20.         {  
  21.             System.out.println("LEAP YEAR");  
  22.         }  
  23.         else  
  24.         {  
  25.             System.out.println("NOT A LEAP YEAR");  
  26.         }  
  27.   }  
  28. }  
Output
 
Gregorian Calendar Class
 
Summary
 
This article explains the Gregorian Calendar class in Java.