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:

/* DateTest.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
 */
import java.util.*;
class DateTest {
   public static void main(String[] a) {
      showDate();
      showCalendar();
      showTimeZone();
   }
   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: "+
                     c.get(Calendar.ZONE_OFFSET)/(60*60*1000));
      System.out.println("Daylight saving offset: "+
                     c.get(Calendar.DST_OFFSET)/(60*60*1000));
   }
   public static void showTimeZone() {
      GregorianCalendar c = new GregorianCalendar();
      TimeZone tz = c.getTimeZone();
      System.out.println("My time: "+c.get(Calendar.HOUR_OF_DAY)+
         ":"+c.get(Calendar.MINUTE));
      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: "+
         c.get(Calendar.HOUR_OF_DAY)+":"+c.get(Calendar.MINUTE));
   }
}

Output:

Time since 01-Jan-1970 00:00:00 GMT: 1708374849660 milliseconds.
Year: 2024
Month: 1
Date: 19
Day of year: 50
Day of week: 2
AM or PM: 1
Hour: 5
Hour of day: 17
Minute: 34
Second: 9
Millisecond: 693
Zone offset: -3
Daylight saving offset: 0
My time: 17:34
My time zone ID: America/Argentina/Buenos_Aires
Los Angeles time: 12:34

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.

Table of Contents

 About This JDK Tutorial Book

 JDK (Java Development Kit)

 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

 Java Logging

 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 - Encryption and Decryption

 The SSL (Secure Socket Layer) Protocol

 SSL Socket Communication Testing Programs

 SSL Client Authentication

 HTTPS (Hypertext Transfer Protocol Secure)

 Outdated Tutorials

 References

 Full Version in PDF/EPUB