Complete Java date and time solutions (3)

Source: Internet
Author: User
Tags time and date

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/libID list of the tzmappings file in the subdirectory. J2se 1.3 only containstzmappingsFile, 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.ZoneInfoObtain 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 classMediumThe source code of the getdefault method is displayed, and it will eventually callsun.util.calendar.ZoneInfoClassgetTimeZoneMethod. This method returnsStringParameters. The Default Time Zone ID is fromuser.timezone(system. Ifuser.timezoneIf it is not defined, it will tryuser.countryAndjava.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,SystemThe property is injava.lang.SystemClassinitPropertiesMethod. 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,SystemProperties are initialized from the Windows registry, while environment variables are initialized in Linux/Unix.initPropertiesThe javadoc declaration of methods. Some attributes must be defined and listed. QuiltJava. util. timezone classgetDefaultOf the three system attributes used by the method, onlyjava.homeAs 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)TimeZoneClass is correct and is suitable for your geographic range (locale. How do you ensure the defaultTimeZoneIs it correct and suitable? This is another new problem. Like most problems, there are also many solutions. Accordingjava.util.TimeZone.getDefaultFrom the source code of the method, the best way is to correctly setuser.timezoneAttribute. When starting a Java virtual machine, you can easily use-DOverride injava.lang.System.initPropertiesValue set in the method. For example:

java -Duser.timezone=Asia/Jerusalem DateTest

This command startsDateTestClass (listed at the beginning of this article), and setuser.timezoneAttributeAsia/Jerusalem. You can also use the java. Lang. system classSetproperty method to setuser.timezoneAttribute:

System.setProperty("user.timezone","Asia/Jerusalem");

If you do not have an available time zone ID, you can create a customTimeZoneUsejava.util.TimeZoneClasssetDefaultMethod to set it to the default time zone-as I previouslyItsInitializerClass.

Remember, in j2se, most date-and time-related classes contain time zone information, including format classes, suchjava.text.DateFormatSo 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

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.