What's your time zone?
Java date and time class (3)
Page 3 of 3
Time zone and Java
Java and Solaris are similar when information about time regions is involved. Each time zone has a time zone ID. In j2se 1.3 and 1.4, this ID is a string that is located in the j2se Installerjre/lib
ID list of the tzmappings file in the subdirectory. J2se 1.3 only containstzmappings
File, but j2se 1.4 contains the time zone data files in different regions of the world.JRE/lib/Zi stores these files.
In j2se 1.4,sun.util.calendar.ZoneInfo
Obtain DST rules from these files. In Solaris, data files in these time regions are stored in binary format, not text files, so you cannot view them. The time zone data files in j2se 1.4 are different from those in Solaris.
Java. util. timezone class
MediumThe source code of the getdefault method is displayed, and it will eventually call
sun.util.calendar.ZoneInfo
ClassgetTimeZone
Method. This method returnsString
Parameters. The Default Time Zone ID is fromuser.timezone
(system
. Ifuser.timezone
If it is not defined, it will tryuser.country
Andjava.home
(System
) To obtain the ID. If it fails to find a time zone ID, it uses a GMT Value of "fallback. In other words, if it does not calculate your time zone ID, it uses GMT as your default time zone.
Note,System
The property is injava.lang.System
ClassinitProperties
Method. This is a local method. Therefore, the source code is unavailable unless you go deep into the local code library in the j2se distribution package. However, in windows,System
Properties are initialized from the Windows registry, while environment variables are initialized in Linux/Unix.initProperties
The javadoc declaration of methods. Some attributes must be defined and listed. QuiltJava. util. timezone class
getDefault
Of the three system attributes used by the method, onlyjava.home
As a "guaranteed" attribute, it is listed in javadoc.
Recommended Solution
Therefore, How do you ensure that Java can give you the correct time and date? In my opinion, the best way is to confirm the default Java Virtual Machine (JVM)TimeZone
Class is correct and is suitable for your geographic range (locale. How do you ensure the defaultTimeZone
Is it correct and suitable? This is another new problem. Like most problems, there are also many solutions. Accordingjava.util.TimeZone.getDefault
From the source code of the method, the best way is to correctly setuser.timezone
Attribute. When starting a Java virtual machine, you can easily use-D
Override injava.lang.System.initProperties
Value set in the method. For example:
java -Duser.timezone=Asia/Jerusalem DateTest
This command startsDateTest
Class (listed at the beginning of this article), and setuser.timezone
AttributeAsia/Jerusalem
. You can also use the java. Lang. system classSetproperty method to set
user.timezone
Attribute:
System.setProperty("user.timezone","Asia/Jerusalem");
If you do not have an available time zone ID, you can create a customTimeZone
Usejava.util.TimeZone
ClasssetDefault
Method to set it to the default time zone-as I previouslyItsInitializer
Class.
Remember, in j2se, most date-and time-related classes contain time zone information, including format classes, suchjava.text.DateFormat
So they will be affected by the default time zone of JVM. However, when you create instances of these classes, you can ensure the correct time zone information for them, making it easier for you to set the default time zone of the entire JVM. Once set, you can ensure that all these classes use the same default time zone. However, the old saying goes, "Everything is unpredictable ".
So try to contact and tame these Java date/time classes!
Translated by willpower, 2003.11.20