Calendar Class and Gregorian Calendar Class in Java

Introduction

The abstract Calendar class provides a set of methods that allows the user to convert a time in milliseconds to several useful components. The GregorianCalendar class implements the normal Gregorian Calendar. 

Calendar

The Calendar defines several protected instance variables. The method areFieldSet is a Boolean that specifies whether the time components have been set. The fields are an array of integers that holds the components. The isSet is a Boolean array that indicates if a specific time component has been set.

Methods defined by Calendar

  1. Abstract void add(int which, int val): Add val to the time or date component specified by which. To subtract, add a negative value, which must be one of the fields defined by the Calendar, such as Calendar.HOUR.
  2. final void clear(int which): Zeros the time component specified in the invoking object.
  3. final int get(int calendarField): This method returns the value of one component of the invoking object. This component is indicated by CalendarField. Eg. Calendar.YEAR, Calendar.MONTH
  4. static locale[] getAvailableLocales(): This method returns an array of Locale objects that contains the locales for which calendars are available.
  5. static Calendar getInstance(TimeZone tz): These methods return a Calendar object for the TimeZone specified by tz. The default locale is used.
  6. static Calendar getInstance(TimeZone tz, Locale locale): These methods return a Calendar object for the TimeZone specified by locale. The default timezone is used.
  7. final void set(int year, int month, int dayOfmonth, int hours, int months, int seconds): Set various date and time components of the invoking object.

Gregorian Calendar

Gregorian calendar class that implements the normal Gregorian Calendar. The getInstance() method of Calendar returns a Gregorian Calendar initialized with the current date and time in the default locale and TimeZone. The two fields of the Gregorian Calendar are AD and BC. These stand for the two eras that the Gregorian Calendar designates. There are also several constructors for the GregorianCalendar objects. The details GregorianCalendar() initializes the object with the current date and time in the default locale and TimeZone.

Constructors Used

The Constructors of this class are given below:

  • GregorianCalendar (int year, int month, int dayofMonth)
  • GregorianCalendar (int year, int month, int dayofMonth, int hours, int minutes)
  • GregorianCalendar (int year, int month, int dayofMonth, int hours, int minutes, int seconds)

The first constructor sets the time to midnight. The second constructor also sets the hours and the minutes. The third constructor adds seconds.

The following constructors create objects initialized with the current date and time using the specified TimeZone and locale.

  • Gregoriancalendae(Locale locale)
  • Gregoriancalendae(TimeZone timeZone)
  • Grgoriancalendae(TimeZone timeZone, Locale locale)
  • GregorianCalendar provides an implementation of all the abstract methods in Calendar. It also provides some additional methods. Perhaps the most interesting is isLeapYear(), which determines if the year is a leap year.

This illustrates in the following example.

Open a new file in the editor and type the following code.

import java.util.*;
class GregorCallDemo
{
public static void main(String args[])
{
String mon[]={
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
"Oct", "Nov", "Dec"};
int year;
GregorianCalendar gcal=new GregorianCalendar();
System.out.println("The date is ");
System.out.println(mon[gcal.get(Calendar.MONTH)]);
System.out.println(""+gcal.get(Calendar.DATE)+"");
System.out.println(year=gcal.get(Calendar.YEAR));


System.out.println("The Time is ");
System.out.println(gcal.get(Calendar.HOUR)+":");
System.out.println(gcal.get(Calendar.MINUTE)+":");
System.out.println(gcal.get(Calendar.SECOND));
 
if(gcal.isLeapYear(year))
{
System.out.println("This is a Leap year");
}
else
{
System.out.println("This is not a Leap Year");
}
}
}
  • Save the file as GregorCallDemo.java
  • Compile the file using javac GregorCallDemo.java
  • Run the file using java GregorCallDemo

The output appears as shown below.

Output

Summary

The abstract Calendar class offers several methods that let the user convert time in milliseconds into a variety of useful components. A calendar that uses the standard Gregorian Calendar is called GregorianCalendar. All of Calendar's abstract methods are implemented by GregorianCalendar.


Similar Articles