Java | Calendar class

10 min read

Calendar class:

Example: Write a program to store objects for representing the dates, in a vector. (Assume joining dates of employees have to be stored).

  • defined in java.util package

'Calendar' class can be used for storing dates and time.

  • A calendar is an abstract class. So, cannot instantiate it.
  • But a reference to the current Instance can be obtained as follows:

Calendar c = Calendar.getInstance( ); ---> Represents the current date & time.

 

Important Methods:

1. int get(int calendarfield)

where the calendar fields are DATE, MONTH, YEAR, HOUR, MINUTE, SECOND. All are static constants. So, access them as Calendar. DATE, Calendar.Hour, etc.

Example:

Calendar c = Calendar.getInstance( ); ---> represents the system's current date & time

System.out.println(c.get(Calendar.DATE)); ---> 22

System.out.println(c.get(Calendar.MONTH)); ---> 9

System.out.println(c.get(Calendar.YEAR)); ---> 2014

System.out.println(c.get(Calendar.HOUR)); ---> 9

System.out.println(c.get(Calendar.MINUTE)); ---> 51

System.out.println(c.get(Calendar.SECOND)); ---> 24

 

2) The values can also be set, using the set( ) method.

void set(Calendarfield, value)

Example:

c.set(Calendar.DATE, 3);

c.set(Calendar.YEAR, 2001); ---> only date, month & year are set

c.set(Calendar.MONTH, 1);

Now:

c.get(Calendar.DATE); ---> 3

c.get(Calendar. SECOND); --->  24 ---> SECOND will have the current 'second' value only as we have not set it.

 

3) boolean after(Calendar obj)

Returns true if the invoking object contains a date that is later than the one specified by Calendar obj.

Example:

C1 .after(C2); ---> true      if C1=21.12.2012 and C2=21.10.2010

 

4) boolean before(Calendar obj)

 

Returns true if the invoking object contains a date that is earlier than the one specified by Calendar.obj 

Example:

C .before(C2); --->true  if C1=10.10.2010 and C2=11.10.2011

 

5) boolean equals(Calendar obj)

Returns true if the invoking object and Calender obj has the same data (including HOUR, MINUTE and SECOND).   

Program:

import java.util.*;
class CalendarMain
{
public static void main(String[ ] args)
{
Vector v = new Vector( );
Calendar c1 = Calendar.getInstance( );
c1.set(Calendar.DATE, 3);
c1.set(Calendar.MONTH, 1);
c1.set(Calendar.YEAR, 2001);
v.add(c1);
Calendar c2 = Calendar.get Instance( );
c2.set(Calenclar.DATE, 10);
c2.set(Calendar.MONTH, 3);
c2.set(Calendar.YEAR, 2002);
v.add(c2);
Calendar c3 = Calendar.getInstance( );
c3.set(Calendar.DATE, 13);
c3.set(Calendar.MONTH, 3);
c3.set(Calendar.YEAR, 2005);
v.add(c3);
for(int i=0;i<v.size( );i++)
{
Calendar c = (Calendar)v.get(i) ;   // the object retrieved should he type casted to the appropriate type while assigning
System.out.println(c.get(Calendar.DATE) + "/" + c.get(Calendar.MONTH) + "/" + c.get(Calendar.YEAR));
}
}
}

Output:

3/1/2001
10/3/2002
13/3/2005
POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error