Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

What's your time zone?

Get a handle on Java date and time classes

  • Print
  • Feedback

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:

  • Print
  • Feedback
What is Tech Briefcase?
TechBriefcase is a new, free service where IT Professionals can Search, Store and Share IT white papers and content like this. Learn more
Bookmark content
Speed up your research efforts with content across the web.
Search and Store
Find the white papers you need. Create folders for any topic.
View Anywhere
Open your briefcase on your iPhone, tablet or desktop. Share with colleagues.
Don't have an account yet?

Resources