class Calendar TimeZone Locale
Summary: After reading this "class Calendar TimeZone Locale" you will come to know the usage of the methods of Date and Calendar class. Also learn to print the date as per timezone or country.
Introduction Date and Calendar
We know many methods of Date class are deprecated; observe the screenshot of earlier program, DateInfo compilation. The problems, thought appropriate, by the designers are overcome with the introduction of java.util.Calendar class in JDK 1.1 version. It is an abstract class. Here, the time is calculated from 01-01-1970. It comes with many variables and methods to manipulate and retrieve date and time particulars.
Being abstract class, the Calendar class cannot be instantiated. Its object can be obtained from its static method getInstance(). The class contains many setXXX() and getXXX() methods apart fields like DAY_OF_WEEK/MONTH/YEAR, HOUR and MINUTE, AM, PM, AM_PM etc. The following table gives the fields (variables) and their purpose. These are useful to get the required information from the Calendar.
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 is the class signature
public abstract class Calendar extends Object implements Serializable, Cloneable, Comparable
The next program uses a few of the above fields.
import java.util.Calendar;
class UsingCalendar
{
public static void main( String agrs[ ] )
{
Calendar today = Calendar.getInstance();
System.out.println("year info: " + today.get(Calendar.YEAR));
System.out.println("Hour info: " + today.get(Calendar.HOUR));
System.out.println("Minutes info: " + today.get(Calendar.MINUTE ));
System.out.println("Day info: " + today.get(Calendar.DAY_OF_MONTH));
System.out.println("Zone info: " + today.get(Calendar.ZONE_OFFSET ));
}
}

Output screen of UsingCalendar.java of class Calendar TimeZone Locale
Calendar today = Calendar.getInstance( );
The static method of Calendar class getInstance() returns an object of Calendar. The variables (fields) like HOUR and YEAR are used which are explained in the previous table.