Calendar Java

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.

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 Reply

Your email address will not be published. Required fields are marked *