Calendar Java

To manipulate dates, Java comes with three classes – Date, Calendar and GregorianCalendar, all from java.util package. Many of Date class methods are deprecated in favor Calendar. But Calendar class is abstract class and more features and methods are given in its subclass GregorianCalendar.
Class signature of Calendar class as defined in java.util package.

public abstract class Calendar extends Object implements Serializable, Cloneable, Comparable

Calendar Java class being abstract cannot be instantiated. Its object is obtained by the static factory method getInstance(). The Calendar Java comes with many methods of type getXXX() and setXXX() to manipulate date or to get current date. Along with methods also comes with fields (instance variables of a class are also known as fields or properties) like AM, PM etc.

The table gives frequently used fields of Calendar Java and their functionality.
Field Name What For
AM Represents AM
PM Represents PM
AM_PM Represents either AM or PM
ERA Represents either BC or AD
MINUTE Represents the minutes part of the hour
SECOND Represents the seconds part of the minute(starts with 1)
MILLISECOND Represents the millisecond part of the second(starts with 1)
HOUR Represents the hour (0 to 12) of either morning or evening
YEAR Represents YEAR
DATE Represents the date of the month (1 to 31)
TIME Displays the system time in milliseconds (after 01-01-1970)
WEEK_OF_YEAR Represents the current week number in the current year
WEEK_OF_MONTH Represents the current week number in the current month
DAY_OF_MONTH Represents the current day number in the month (starts with 1)
DAY_OF_YEAR Represents the current day number in the year (starts with 1)
DAY_OF_WEEK Prints the day of the current week (like Sunday, Monday etc.)
Following example on Calendar Java uses the common methods.
import java.util.Calendar;
public class CalendarJava
{
  public static void main( String agrs[ ] )
  {
    Calendar currentDate = Calendar.getInstance();

    System.out.println("Date part of current date: " + currentDate.get(Calendar.DATE));
    System.out.println("Hour part of current date: " +  currentDate.get(Calendar.HOUR));
    System.out.println("Minutes part of current date: " +  currentDate.get(Calendar.MINUTE));
    System.out.println("Seconds part of current date: " +  currentDate.get(Calendar.SECOND));
    System.out.println("Milliseconds part of current date: " +  currentDate.get(Calendar.MILLISECOND));
    System.out.println("Year part of current date: " + currentDate.get(Calendar.YEAR));

    System.out.println("\nWhich Day of week of current date: " +  currentDate.get(Calendar.DAY_OF_WEEK));
    System.out.println("Which Day of month current date: " +  currentDate.get(Calendar.DAY_OF_MONTH));
    System.out.println("Which Day of year of current date: " + currentDate.get(Calendar.DAY_OF_YEAR));

                                                       // integer offset number is used to convert current time to GMT
    System.out.println("\nZone Offset Particulars: " + currentDate.get(Calendar.ZONE_OFFSET));

                                                       // 0 represents AM and 1 represents PM
    System.out.println("\nThis is AM or PM)?: " + currentDate.get(Calendar.AM_PM));
   }
}

ssOutput screen of Calendar java

Calendar currentDate = Calendar.getInstance();

The getInstance() static method of Calendar class returns an object of Calendar, her it is currentDate. With currentDate many fields are called get information of system current date.

===================For Extra Reading=============================
A. Basic Programs

1. class Date
2. class Calendar (TimeZone and Locale)
3. class GregorianCalendar

B. Conversions between java.util.Date, java.util.Calendar and java.sql.Date

1. Date Calendar Conversions

Leave a Comment

Your email address will not be published.