Java date and time class completely solved (1)

zhaozj2021-02-16  45

What's your time zone?

Java date and time class completely solved (1)

Page 1 of 3

Are you struggling in the date and time in the Java language? When you display the date and time on your computer, are you going to be alive? Or maybe it takes an hour? Or two hours, or more serious? When you try to write a date and time in a file, or go to your database (through Java Database Connectivity (JDBC)) - the error time is saved?

I have been trapped for a long time by this problem. I can't solve why Java changed my timestamps. I retrieve timestamp data from the database and displayed in my graphical user interface (GUI), always display a different time - and I expect to differ by 1, 2 or 3 hours. I retrieve the value in the database, it is normal. So what should I do?

The survey finally decided to investigate this situation. First of all, 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 ());}}

Java 2 Platform, Standard Edition (J2SE) 1.3.1_01, I get: STANDARD Edition (J2SE)

Date = Tue May 06 08:13:17 IDT 2003Calendar = 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 2003Calendar = 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 2003Calendar = 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 have seen it, Calendar is similar to a class member is an instance of java.util.simpletimezone, and I can also determine through some ways:

Using the Javap utility, it is part of J2SE: javap -private java.util.calendar Checks the reflex mechanism of the source code utility Java available in the src.jar file in J2SE

In either case, you will find a private instance member in the java.util.calendar class, name is Zone, which is a java.util.timezone instance. Part of the output of javap results is displayed:

Private java.util.timezone Zone

When I use the same approach to the Java.util.Date class, you can also find it has the following members:

Private transient java.util.calendar Cal;

This is indirectly indicated that the DATE is also a Timezone member.

However, Java document tells us that Timezone is an abstract class, but SimpleTimezone is a polymerization. Therefore, although it is defined as a member, the ZONE member in Calendar is actually a SimpleTimezone instance (in J2SE 1.3). This can be easily confirmed by investigating Timezone using the above method. Virtually, the Zone member in Calendar is a SimpleTimezone instance. Check the output of the DateTest class, which involves the Timezone's Daylight Sunditation (DST) attribute and is named the following properties:

Dstsavings Usedaylight Startyear StartMode Startmonth Startday Startdayofweek StartTime StartTimeMode Endmove Endday Enddayofweek endtime endtimode

So you can see that Date and Calendar have a concept on Daylight Saving Time. When I started to investigate this, I was Summer (Last Year), in order to adjust all of our Server All Summertime (DST), I took the system time forward for an hour. So I think Java will not adjust the DST.

Translated by Willpower, 2003.11.17

转载请注明原文地址:https://www.9cbs.com/read-24931.html

New Post(0)