JDK's TIMEZONE__JDK

Source: Internet
Author: User
Tags time zones locale time and date unique id
Originally see calendar This class, but found this class inside many places use of time zone this class, not very clear this class role, so ran to see timezone. As the
name suggests, time zone this class is used to control the timezone, the concept of time zone learned in junior High school, what the United States East eight, and so on, due to different time zones different times, so this class in the Java time related classes are heavily used.

Let's take a look at the AP for this class:

Static string[] Getavailableids () gets all the time zone IDs, the time zone ID is a string, and each time zone has a unique ID, for example: etc/gmt+10 static string[] Getavailableids ( int rawoffset) Gets the ID of the time zone based on jet lag, meaning which time zones are given the value static TimeZone Getdefault () Gets the time zone instance String getdisplayname the current machine () Gets the name of the time zone , such as: China Standard Time String GetDisplayName (Boolean daylight, int style) string GetDisplayName (Locale Locale) string GetDisplayName ( Boolean daylight, int style,locale Locale) above three can get the name of the time zone according to different format int getdstsavings () Returns The amount of of
To the local standard time to get the local wall clock time. This method does not know what to do. String GetID () gets the ID abstract int getoffset (int era, int, int month, int day, int dayofweek, int milliseconds) for the time zone instance to get the given Time difference int GetOffset (long date) abstract int getrawoffset () static TimeZone getTimeZone (String ID) Boolean hassamerules (Tim
EZone other) Determines whether the time difference between the timing area and the current timezone is the same Boolean observesdaylighttime () static void SetDefault (TimeZone zone) void SetID (String ID)
 abstract void Setrawoffset (int offsetmillis) Abstract Boolean usedaylighttime ()

Run a single example:

public static void Main (string[] args) {
        TimeZone timezone=timezone.getdefault ();
        String[] Ids=timezone.getavailableids ();
        for (int i=0,length=ids.length;i<length;i++)
        {
            System.out.println (ids[i]);
        }
        System.out.println (Timezone.getrawoffset ());
        System.out.println (Timezone.getdisplayname ());
        Timezone=timezone.gettimezone ("Australia/canberra");
        System.out.println (Timezone.getrawoffset ());
        System.out.println (Timezone.getdisplayname ());
    }
The result:

....

Time zone IDs

...

28800000
China Standard Time
36000000
Eastern Standard Time (NSW)

The final implementation of this class must be called to the native method, but there is no need to delve into it and know how to use it.


Then post a description of time zone that you see elsewhere:

Java and Solaris are similar when it comes to information about time zones. Each time area has a time area ID identifier. In J2SE 1.3 and 1.4, this ID is a string and is a list of these IDs by tzmappings files located in the Jre/lib subdirectory of the J2SE installer. J2SE 1.3 contains only tzmappings files, but J2SE 1.4 contains data files for time zones in different parts of the world. Jre/lib/zi store these files. In J2SE 1.4, Sun.util.calendar.ZoneInfo obtains DST rules from these files. In Solaris, these time zone data files are stored in binary form, not as text files, so you can't look at them. The time zone data files in J2SE 1.4 are different from those in Solaris.

The source code for the Getdefault method in the Java.util.TimeZone class shows that it will eventually invoke the getTimeZone method of the Sun.util.calendar.ZoneInfo class. This method returns a string parameter as an ID for the required time range. This default time zone ID is obtained from the User.timezone (System) attribute. If User.timezone is not defined, it attempts to obtain an ID from the User.country and Java.home (System) properties. If it does not successfully find a time area ID, it uses a "fallback" GMT value. In other words, if it does not compute your time area ID, it will use GMT as your default time zone.

Note that the system property is initialized in the InitProperties method of the Java.lang.System class. This is a local method. Therefore, the source code is not available----unless you delve into the local code base in the J2SE distribution. However, in Windows systems, the system properties are initialized from the Windows registry and are initialized by environment variables in Linux/unix. The Javadoc declaration of the InitProperties method, some attributes "must be guaranteed to be defined" and list them. Of the three system properties used by the Getdefault method of the Java.util.TimeZone class, only Java.home is listed as a "guaranteed" attribute in Javadoc.

Recommended Solutions:
So how do you make sure that Java can give you the right time and date? The best way to do this is to make sure that the default timezone class for the Java Virtual Machine (JVM) is correct and is appropriate for your geographic scope (Locale). How do you make sure that the default timezone is correct and appropriate? This is a new problem again. As with most of the problems dealt with, this also has a number of solutions. According to the source code of the Java.util.TimeZone.getDefault method, the best way is to set the User.timezone property correctly. When you start a Java Virtual machine, you can easily override (override) the values set in the Java.lang.System.initProperties method by using the-d command-line parameters. For example:

1 Java-duser.timezone=asia/shanghai Datetest

This command starts the Datetest class and sets the User.timezone property to Asia/shanghai. You can also set the User.timezone property by using the SetProperty method of the Java.lang.System class:

1 System.setproperty ("User.timezone", "Asia/shanghai");

If you don't have an available time zone ID for you, then you can create a custom TimeZone using the SetDefault method of the Java.util.TimeZone class to set it as the default time zone----Just like I was in Itsinitializer In the same way as you do in a class.

Remember, in J2SE, most date and time related classes contain time zone information, including those format classes, such as Java.text.DateFormat, so they are all affected by the JVM's default time zone. 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 for the entire JVM. And once you've set it up, you can make sure that all of these classes will use the same default time zone.





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.