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

(Continued from previous part...)

The following program shows some features of the time related classes:

/**
 * DateTest.java
 * Copyright (c) 2002 by Dr. Herong Yang
 *
 * This program shows you some basic features of date related classes.
 */
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: 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.

Calendar Manipulations

Objects of Date class are easy to be manipulated, since each of them has only one numerical field in unit of millisecond. For example, to calcualate the number of full days between two Date objects, you can use the following expression:

   (date1.getTime() - date2.getTime()) / (1000*60*60*24)

However, objects of Calendar are not so easy to be manipulated, since each of them has many related numerical fields in different calendar units. For example, incrementing the day-in-month field by 1, may affect the month field, if the Calendar object is at the end of a month; then the change in the month field may affect the year field, if the Calendar object is at the end of a year.

To help us manipulate Calendar objects safely, JDK offers the following method:

   add(int field, int delta)

with two rules:

  • The new value of the specified field is the old value plus the delta, and modulo any overflow. If overflow occurs, the next higher field will incremented or decremented.
  • If the new value of the specified field is causing changes on the value range of the next lower field, that field needs to be adjusted.

(Continued on next part...)

Part:   1  2   3 

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