java.util.Calendar - The Abstract Calendar Class

This section provides a tutorial example on how to use calendar related classes: java.util.Calendar, java.util.GregorianCalendar, java.util.TimeZone, and java.util.SimpleTimeZone.

The Calendar class, java.util.Calendar, is an abstract base class, providing a foundation for subclasses to represent a specific instance in time, in a specific calendar system.

The GregorianCalendar class, java.util.GregorianCalendar, is a concrete subclass of the Calendar class, representing a specific instance in time in Gregorian Calendar system with time zone and daylight saving adjustments.

This abstract base class and concrete subclass structure is very useful for future implementations of other calendar systems, like the Chinese lunar calendar system.

The TimeZone class, java.util.TimeZone, is an abstract base class, providing a foundation for subclasses to represent a particular time zone system.

The SimpleTimeZone class, java.util.SimpleTimeZone, is a concrete subclass of the TimeZone class, representing a time zone for use with the GregorianCalendar class.

I don't know why we need this base class and subclass structure to implement the time zone system. How many time zone systems are there? I only know one.

The following program shows some features of calendar related classes:

 - Copyright (c) 2014,, All Rights Reserved.
import java.util.*;
class DateTest {
   public static void main(String[] a) {
   public static void showDate() {
      Date now = new Date(); // the current time
      long t = now.getTime(); 
      System.out.println("Time since 01-Jan-1970 00:00:00 GMT: " + 
         t + " milliseconds.");
   public static void showCalendar() {
      GregorianCalendar c = new GregorianCalendar(); 
         // the current time represented in the Gregorian calendar
         // in local time zone and daylight saving adjustments
      System.out.println("Year: "+c.get(Calendar.YEAR));
      System.out.println("Month: "+c.get(Calendar.MONTH));
      System.out.println("Date: "+c.get(Calendar.DATE));
      System.out.println("Day of year: "+c.get(Calendar.DAY_OF_YEAR));
      System.out.println("Day of week: "+c.get(Calendar.DAY_OF_WEEK));
      System.out.println("AM or PM: "+c.get(Calendar.AM_PM));
      System.out.println("Hour: "+c.get(Calendar.HOUR));
      System.out.println("Hour of day: "+c.get(Calendar.HOUR_OF_DAY));
      System.out.println("Minute: "+c.get(Calendar.MINUTE));
      System.out.println("Second: "+c.get(Calendar.SECOND));
      System.out.println("Millisecond: "+c.get(Calendar.MILLISECOND));
      System.out.println("Zone offset: "+
      System.out.println("Daylight saving offset: "+
   public static void showTimeZone() {
      GregorianCalendar c = new GregorianCalendar(); 
      TimeZone tz = c.getTimeZone();
      System.out.println("My time: "+c.get(Calendar.HOUR_OF_DAY)+
      System.out.println("My time zone ID: "+tz.getID());
      tz = TimeZone.getTimeZone("America/Los_Angeles");
      c.setTimeZone(tz); // changing the time zone
      System.out.println("Los Angeles time: "+


Time since 01-Jan-1970 00:00:00 GMT: 1035236526708 milliseconds.
Year: 2002
Month: 9
Date: 21
Day of year: 294
Day of week: 2
AM or PM: 1
Hour: 5
Hour of day: 17
Minute: 42
Second: 6
Millisecond: 738
Zone offset: -5
Daylight saving offset: 1
My time: 17:42
My time zone ID: America/New_York
Los Angeles time: 14:42

The GregorianCalendar is a tool that takes three inputs: an instance of time, a time zone system, and daylight saying adjustment, and converts them into calendar related information, such as the month, the date, the day of week, and the hour of day. But in this program, I am using the default constructor, GregorianCalendar(), to take the current time, and the time zone and daylight saving adjustment settings on the operating system, where this program was executed.

From the output of the program, you can see that, my computer is set to east coast time zone, with one hour daylight saving offset.

Last update: 2014.

Table of Contents

 About This JDK Tutorial Book

 Downloading and Installing JDK 1.8.0 on Windows

 Downloading and Installing JDK 1.7.0 on Windows

 Downloading and Installing JDK 1.6.2 on Windows

 Java Date-Time API

Date, Time and Calendar Classes

 java.util.Date - JDK Class to Measure Date and Time

java.util.Calendar - The Abstract Calendar Class

 java.util.Calendar.add() - Calendar Manipulation Method

 Date and Time Object and String Conversion

 Number Object and Numeric String Conversion

 Locales, Localization Methods and Resource Bundles

 Calling and Importing Classes Defined in Unnamed Packages

 HashSet, Vector, HashMap and Collection Classes

 Character Set Encoding Classes and Methods

 Character Set Encoding Maps

 Encoding Conversion Programs for Encoded Text Files

 Socket Network Communication

 Datagram Network Communication

 DOM (Document Object Model) - API for XML Files

 SAX (Simple API for XML)

 DTD (Document Type Definition) - XML Validation

 XSD (XML Schema Definition) - XML Validation

 XSL (Extensible Stylesheet Language)

 Message Digest Algorithm Implementations in JDK

 Private key and Public Key Pair Generation

 PKCS#8/X.509 Private/Public Encoding Standards

 Digital Signature Algorithm and Sample Program

 "keytool" Commands and "keystore" Files

 KeyStore and Certificate Classes

 Secret Key Generation and Management

 Cipher - Secret Key Encryption and Decryption

 The SSL (Secure Socket Layer) Protocol

 SSL Socket Communication Testing Programs

 SSL Client Authentication

 HTTPS (Hypertext Transfer Protocol Secure)

 Outdated Tutorials


 PDF Printing Version