What's your time zone?
Java date and time class (2)
Page 2 of 3
First try the solution
Based on Java Document Information andOutput result of the datetest class,
I made the best bet: the Java Virtual Machine automatically sets a default time zone when it is run for the first time ). For verification, I created an ITSInitializer
Class so that my application can run it when it is loaded (launched. Here is my first attempt:
import java.util.TimeZone;
import java.util.SimpleTimeZone;
public class ItsInitializer {
private static boolean s_initialized = false;
private ItsInitializer() {
}
public static synchronized void initialize() {
if (!s_initialized) {
// Modifies the default time zone, disables the Daylight Saving Time.
SimpleTimeZone dtz = (SimpleTimeZone) TimeZone.getDefault();
dtz.setStartRule(0,0,0,0);
dtz.setEndRule(0,0,0,0);
TimeZone.setDefault(dtz);
s_initialized = true;
}
}
}
In other words, I changed the default JVMTimeZone,
Therefore, it does not have a daylight saving time (DST) rule, so you don't have to worry about adjusting it.
Later, j2se 1.4 was released, so I upgraded it. But what's unexpected is:Its itsinitializer class cannot run.
So I will investigate again immediately. AOutput of the datetest class (
For j2se 1.4.1 _ 01 on Windows 98 ):
Date = Tue May 06 05:31:03 IDT 2003
Calendar = java.util.GregorianCalendar[time=1052188263870,areFieldsSet=true,areAllFieldsSet
=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Jerusalem",offset=7200000,
dstSavings=3600000,useDaylight=true,transitions=143,lastRule=java.util.SimpleTimeZone
[id=Asia/Jerusalem,offset=7200000,dstSavings=3600000,useDaylight=true,startYear=0,
startMode=1,startMonth=3,startDay=1,startDayOfWeek=0,startTime=3600000,startTimeMode=0,
endMode=1,endMonth=9,endDay=1,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=5,HOUR_OF_DAY=5,MINUTE=31,SECOND=3,MILLISECOND=870,ZONE_OFFSET=7200000,
DST_OFFSET=3600000]
As you can see, the timezone class in j2se 1.4The zone Member is no longer
SimpleTimeZone
. Insteadsun.util.calendar.ZoneInfo
Class. Therefore, I need to change its initializer to be compatible with the j2se 1.4 platform:
import java.util.TimeZone;
import java.util.SimpleTimeZone;
public class ItsInitializer {
private static boolean s_initialized = false;
private ItsInitializer() {
}
public static synchronized void initialize() {
if (!s_initialized) {
// Modifies default time zone, disables Daylight Saving Time.
TimeZone l_defaultTimeZone = TimeZone.getDefault();
int l_rawOffset = l_defaultTimeZone.getRawOffset();
String l_id = l_defaultTimeZone.getID();
SimpleTimeZone l_simpleTimeZone = new SimpleTimeZone(l_rawOffset,
l_id,
0,
0,
0,
0,
0,
0,
0,
0);
TimeZone.setDefault(l_simpleTimeZone);
s_initialized = true;
}
}
}
I created a rule without DST.SimpleTimeZone
Instance, and assign it as the default JVMTimeZone
. The second execution form is better than the first one because it does not consider an actual class for the zone member of the calendar class. Note that the second version is backward compatible-it works well on j2se 1.3. Nothing is more rewarding than learning through personal experience. I want to gain more experience and then completely solve this problem.
In my opinion, during DTS, we always physically adjust the time of the computer, so we never have to adjust DST, in this way, the JVM always sets a default time zone without a timeout rule. The problem is solved.
So we were happy to develop our applications through the above policies, and everything went smoothly. There is no conflict or difference between date and time. When winter comes, we will reset the computer time. We have not encountered any date or time issues in the program. Thank you very much.ItsInitializer
This class.
However, later I found that I made another mistake, so that I had to go back to the original annoyance.
An unpredictable fault
Winter is over, and summer is coming again. Naturally, it enters the seasonal stage. I thought it was okay, so I used the above policy to solve it.
On the very day we moved to DST, I got timestamp discrepancies. What happened?
This year, I did not physically adjust the computer's time. I have configured our sun server so that we do not need to adjust the system clock physically.The date command is
Unix/Linux commands are used to display the date and time of the current system.date
The Solaris version of TThe imezone configuration file helps automatically adjust the DST display. And
JavaThe working mechanism of the calendar class is similar.
Therefore, if the timezone configuration file is installed, you do not need to adjust the system clock physically. I believe Linux can do the same.
We run Windows XP on Windows, and we do not physically adjust the system time. We can configure the time zone in Windows XP. The problem arises. For DST, sometimes it will be automatically adjusted and sometimes it will not be adjusted. The following two figures show this:
Figure 1. Time Zone with automatic DST Adjustment |
Figure 2. Time Zone without automatic ic dst Adjustment |
Translated by willpower, 2003.11.18