JDK Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 4.32, 2006

Time and Calendar

Part:   1   2  3 

JDK Tutorials - Herong's Tutorial Notes © Dr. Herong Yang

Internationalization

Character Set and Encoding

Socket Communication

Document Object Model (DOM)

XSD Validation in Java

XSL - Transformer in Java

JCA - Private and Public Key Pairs

JCE - Secret Key

SSL (Secure Socket Layer)

SSL - Client Authentication

... Table of Contents

Sample programs listed in this chapter have been tested with JDK 1.3.1, 1.4.1 and 1.5.0.

Time or Date

Time usually refers to the hour, the minute and may be the second of a specific moment within a day. For example, if someone asks you: "What time is it?", you would answer: "It's nine thirty.", meaning that 9 hours and 30 minutes passed away since the beginning of the day.

Date usually refers to the calendar date of a specific day within a year. For example, if someone ask you: "What date is it today?", you would answer: "Today is October 25.", meaning that today is 21st day of the 10th month of this year.

So both terms: "time" and "date" are measurements of a specific moment. "Time" measures a specific moment with a unit of minute or second, using the beginning of the day as a reference point. "Date" measures a specific moment with a unit of day, using the beginning of the year as a reference point. Notice that these types of measurements are calendar and time zone dependent. At exactly the same moment, the values of "time" and "date" are different from one country to another.

In JDK, we decided to use only one class, the Date class (java.util.Date), to measure a specific moment with a unit of millisecond, using a fixed moment of January 1, 1970, 00:00:00 GMT as a reference point.

The Date class measurement is calendar and time zone independent. So if you run the following code at exactly the same moment any where in the world, you will get exactly the same value:

   Date now = new Date(); // what time is it?
   long t = now.getTime(); 
   System.out.println("Time since 01-Jan-1970 00:00:00 GMT: " + 
      t + " milliseconds.");

Output:

Time since 01-Jan-1970 00:00:00 GMT: 1035236526708 milliseconds.

The Date class is very convenient for measuring a period of time:

   long t1 = (new Date()).getTime();
   // performing task XYZ
   long t2 = (new Date()).getTime();
   System.out.println("It took about " + ((t2-t1)/1000) + " seconds to"
      + " finish XYZ.");

However, the Date class does not provide any calendar and time zone related information. It requires the help of the Calendar class, see the next section.

The following is what you should remember about date and time in JDK:

  • The Date class actually measures time.
  • The Date class does not measure calendar date and hour.
  • There is no Time class.

Calendar and Time Zone

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.

(Continued on next part...)

Part:   1   2  3 

Dr. Herong Yang, updated in 2006
JDK Tutorials - Herong's Tutorial Notes - Time and Calendar