Java Date and Time Help Files – Part One

What’s Your Time Zone?

As a website designer, you need to get a handle on Java date and time classes. Are you struggling with dates and times in Java? Does Java show you a time you do not expect? Learn how to tame these so they do what you want. (1,900 words; October 3, 2003)

Writing Dates and Times Using JavaHow to write Java date and time

Are you struggling with dates and times in your Java programs? When you display date and time data on the computer screen, is it an hour behind what it should be?

Or maybe it’s an hour ahead, or two hours behind, or worse? When you try to write dates and times to files—or to your database (via Java Database Connectivity (JDBC))—from your Java programs, is the wrong time saved?

I was plagued by these problems for a long time. I couldn’t figure out why Java changed the timestamps I gave it.

I selected timestamp data from the database and displayed it in my graphical user interface (GUI), where, lo and behold, it would show a different time—one, two, or three hours different from what I expected.

I rechecked the value in the database, and it was correct. What on earth was going on?

The investigation

Eventually I decided to investigate this situation. First, I wrote a simple Java class:

import java.util.*;
public class DateTest {
public static void main(String[] args) {
System.out.println(“Date = ” + new Date());
System.out.println(“Calendar = ” + Calendar.getInstance());
}
}

On Windows 98 with Java 2 Platform, Standard Edition (J2SE) 1.3.1_01, I got:

Date = Tue May 06 08:13:17 IDT 2003
Calendar = java.util.GregorianCalendar[time=1052197997184,areFieldsSet=true,areAllFieldsSet
=true,lenient=false,zone=java.util.SimpleTimeZone[id=Asia/Jerusalem,offset=7200000,
dstSavings=3600000,useDaylight=true,startYear=0,startMode=1,startMonth=3,startDay=9,
startDayOfWeek=0,startTime=3600000,startTimeMode=0,endMode=1,endMonth=8,endDay=24,
endDayOfWeek=0,endTime=3600000,endTimeMode=0],firstDayOfWeek=1,minimalDaysInFirstWeek=1,
ERA=1,YEAR=2003,MONTH=4,WEEK_OF_YEAR=19,WEEK_OF_MONTH=2,DAY_OF_MONTH=6,
DAY_OF_YEAR=126,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=8,HOUR_OF_DAY=8,
MINUTE=13,SECOND=17,MILLISECOND=184,ZONE_OFFSET=7200000,DST_OFFSET=3600000]

On Sun Solaris 7 with J2SE 1.3.1_02, I got:

Date = Tue May 06 08:13:17 IDT 2003
Calendar = java.util.GregorianCalendar[time=1052197997184,areFieldsSet=true,areAllFieldsSet
=true,lenient=false,zone=java.util.SimpleTimeZone[id=Asia/Jerusalem,offset=7200000,
dstSavings=3600000,useDaylight=true,startYear=0,startMode=1,startMonth=3,startDay=9,
startDayOfWeek=0,startTime=3600000,startTimeMode=0,endMode=1,endMonth=8,endDay=24,
endDayOfWeek=0,endTime=3600000,endTimeMode=0],firstDayOfWeek=1,minimalDaysInFirstWeek=1,
ERA=1,YEAR=2003,MONTH=4,WEEK_OF_YEAR=19,WEEK_OF_MONTH=2,DAY_OF_MONTH=6,
DAY_OF_YEAR=126,
DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=8,HOUR_OF_DAY=8,
MINUTE=13,SECOND=17,
MILLISECOND=184,ZONE_OFFSET=7200000,DST_OFFSET=3600000]

And on Linux Mandrake 7.2 with J2SE 1.3.0, I got:

Date = Mon May 05 21:04:32 GMT+00:00 2003
Calendar = java.util.GregorianCalendar[time=1052168673155,areFieldsSet=true,areAllFieldsSet
=true,lenient=true,zone=java.util.SimpleTimeZone[id=Custom,offset=0,dstSavings=3600000,
useDaylight=false,startYear=0,startMode=0,startMonth=0,startDay=0,startDayOfWeek=0,
startTime=0,startTimeMode=0,endMode=0,endMonth=0,endDay=0,endDayOfWeek=0,endTime=0,
endTimeMode=0],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2003,MONTH=4,
WEEK_OF_YEAR=19,WEEK_OF_MONTH=2,DAY_OF_MONTH=5,DAY_OF_YEAR=125,
DAY_OF_WEEK=2,
DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=9,HOUR_OF_DAY=21,MINUTE=4,
SECOND=33,MILLISECOND=155,
ZONE_OFFSET=0,DST_OFFSET=0]

As you can see, the Calendar class seems to have a class member that is a java.util.SimpleTimeZone instance. I can confirm this in several ways:

Use the javap utility, which is part of J2SE, like so:

javap -private java.util.Calendar

Examine the source code, which is available in the src.jar file included in J2SE

Use Java’s reflection mechanism

In any case, you will discover that the java.util.Calendar class has a private instance member named zone that is a java.util.TimeZone instance, as this part of javap’s output shows:

private java.util.TimeZone zone

When I try the same trick with the java.util.Date class, you can see it has the following instance member:

private transient java.util.Calendar cal;

This means that, indirectly, the Date class also has a TimeZone member.

However, the Javadocs tell us that TimeZone is an abstract class, while SimpleTimeZone is a concrete subclass. Therefore, despite the member definition, the zone member in Calendar is actually a SimpleTimeZone instance (in J2SE 1.3).

This can be easily confirmed by investigating the TimeZone class using the methods described above. Indeed, the zone member in Calendar is a SimpleTimeZone instance.

Examining the DateTest class’s output,

it looks like TimeZone has attributes that relate to Daylight Saving Time (DST), namely the following attributes:

  • dstSavings
  • useDaylight
  • startYear
  • startMode
  • startMonth
  • startDay
  • startDayOfWeek
  • startTime
  • startTimeMode
  • endMode
  • endMonth
  • endDay
  • endDayOfWeek
  • endTime
  • endTimeMode

    So as you can see, the Date and Calendar classes have a notion regarding Daylight Saving Time.

    When I started investigating this, it was summer (last year), and in order to adjust for DST on all our servers, we physically moved the system clocks forward one hour.

    Therefore, I figured that Java wouldn’t make adjustments for DST.

Continue to Java Date and Time Part 2 >>>

Learn Java date and timeHow to Design Websites for Beginners

To learn more about website designing check out Shelley Lowery’s course here..

Filed under: Web Design