Java date and time class (1)

Source: Internet
Author: User

What's your time zone?

Java date and time class (1)

Page 1 of 3

Are you struggling with dates and times in Java? Does it take an hour for you to display the date and time on your computer? Or an hour earlier ?, Or two hours, or more serious? When you try to use Java to write the date and time to a file, or to your database (through Java database connectivity (JDBC)-is the error time saved?

I have been troubled by this problem for a long time. I cannot solve Why JAVA changed the timestamps I gave ). When I retrieve timestamp data from the database and display it in my graphical user interface (GUI, it will always show a different time-a difference of 1, 2 or 3 hours from my expected value. I re-checked the value in the database. It is normal. So what should we do?

Investigation
In the end, 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());
  }
}

In Windows 98 Java 2 platform, Standard Edition (j2se) 1.3.1 _ 01, I get:

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]

In Sun Solaris 7 with j2se 1.3.1 _ 02, I get:

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]

In Linux Mandrake 7.2 With j2se 1.3.0, I get:

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]

You see,CalendarA class member seems to beAn instance of Java. util. simpletimezone,In addition, I can determine through some methods:

  1. Use the javap utility, which is part of j2se:

    javap -private java.util.Calendar

     

  2. Check j2seAvailable source code in the SRC. JAR File

  3. Practical Java reflection mechanism

In any case, you will find that there is a private instance Member in the Java. util. Calendar class named zone, which is a Java. util. timezone instance. Some of the javap results are displayed as follows:

private java.util.TimeZone zone

Whenjava.util.DateWhen the same method is adopted for a class, you can also find that it has the following members:

private transient java.util.Calendar cal;

This indirectly indicates that,DateClass is also a timeZoneMember.

However, the Java document tells usTimeZoneIs an abstract class,SimpleTimeZoneIs an aggregation subclass. Therefore, even if it is defined as a memberA zone member is actually a simpletimezone instance,(In j2se 1.3 ). This can be easily confirmed by investigating timezone using the above method. Truly,In the calendar arZone MemberIsSimpleTimeZoneInstance. CheckDateTestClass output, which involves the timezone Runtime (DST) Attribute and names them as the following attributes:

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

So you can see,DateAndCalendarClass has a concept in daylight saving time. When I started investigating this, it was summer (last year). In order to adjust all the clock times on our server (DST), I moved the system time one hour forward. Therefore, I don't think Java will adjust DST.

Translated by willpower, 2003.11.17

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.