Way2Java

class Calendar TimeZone Locale

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.

Understanding Time Zone

The earth is demarcated into 24 regions and each region is given a name like India Time Zone (I) and Christmas Island Time (CXT) etc. Each region constitutes two longitudes separated by 15 degrees and is known as local time for that region. The local time will be the same for the whole region. The time difference with the adjacent regions is 1 hour. The local time is an offset from the Greenwich Mean Time (also known as UTC, Coordinated Universal Time).

Perception of Locale

A locale gives the particulars of a locality like language, cultural habits, currency and the country etc. An operating system comes by default loaded with the locale as Locale Identifier (LCID) like Locale.bg for Bulgaria etc.

Using locale in Java (class Calendar TimeZone Locale)

Suppose we would like to know about Bulgaria locale. Following is the snippet of code.

SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy HH:mm", Locale.bg);
Date date = format.parse("12-Jan-2011 10:45");
System.out.println(date);

The above statement prints the Bulgarian date.