Go to: JVM time zone settings

Source: Internet
Author: User
Tags time zones
From: http://hi.baidu.com/iammonster/blog/item/d4b386de053c0f5ecdbf1a1b.html

Today, I encountered a problem: it took 8 hours to upload data from the local machine to another machine.

When using Java Network Programming, note the time zone. User. timezone

You can use the following code to traverse the current system attributes.
Java code 

  1. //------------------------------------------------------------------------------
  2. Final properties P = system. getproperties ();
  3. Final enumeration E = P. Keys ();
  4. While (E. hasmoreelements ())
  5. {
  6. Final string PRT = (string) E. nextelement ();
  7. Final string prtvalue = system. getproperty (PRT );
  8. System. Out. println (PRT + ":" + prtvalue );
  9. }
  10. //--------------------------------------------------------------------------------
//------------------------------------------------------------------------------final Properties p = System.getProperties();        final Enumeration e = p.keys();        while (e.hasMoreElements())        {            final String prt = (String) e.nextElement();            final String prtvalue = System.getProperty(prt);            System.out.println(prt + ":" + prtvalue);        }//--------------------------------------------------------------------------------

The output contains user. timezone, which is the time zone of the current JVM system. You need to check whether the time zone on the running machine is consistent, so it is best to set the code uniformly when using the time.

Java code  
  1. //--------------------------------------------------------------------------
  2. Final timezone zone = timezone. gettimezone ("GMT + 8 ");
  3. //-------------------------------------------------------------------------
//--------------------------------------------------------------------------final TimeZone zone = TimeZone.getTimeZone("GMT+8");//-------------------------------------------------------------------------

Set the default time zone. Of course you can check it like this:
Java code 

  1. //---
  2. System. Out. println (timezone. getdefault (); // output the current Default Time Zone
  3. Final timezone zone = timezone. gettimezone ("GMT + 8"); // obtain the China Time Zone
  4. Timezone. setdefault (zone); // sets the time zone
  5. System. Out. println (timezone. getdefault (); // output verification
  6. //---
// --- System. out. println (timezone. getdefault (); // output the current default time zone final timezone zone = timezone. gettimezone ("GMT + 8"); // obtain the China time zone. setdefault (zone); // set the time zone system. out. println (timezone. getdefault (); // output verification //---

In addition to the above solution, you can also:

Add parameters for running Java in Tomcat
-Duser. timezone = Asia/Shanghai
System. setproperty ("user. timezone", "Asia/Shanghai ");

But to solve the time zone problem, encoding is too annoying, so you need to know how to fundamentally solve the problem:

In Linux: JRE obtains the time zone information from the/etc/sysconfig/clock file. You can modify the time zone information by adding a file if no time zone information exists.

Content of the clock file:

Zone = "Asia/Shanghai"

UTC = false

Arc = false

Zone -- Time Zone

UTC -- indicates that the clock is set to UTC.

Arc -- used for Alpha only to indicate that arc is used.

However, in Windows, it is said that drive: \ Program Files \ common files \ Microsoft
Shared \ Web Server Extensions \ 60 \ config
Unfortunately I did not deal with it, refer to: http://support.microsoft.com/kb/888253/zh-cn

Supplement: Solution for Windows:

Check the Registry: Software \ Microsoft \ Windows NT \ CurrentVersion \ time zones
Is there a problem? My problem is that there are many missing ones. When I read the registry from the bottom layer of Java, I stopped reading the Chinese Standard Time. The default GMT standard time is used, time Difference from my machine
8 hours.

You can find a good registry information and export it. You can also delete the unused time zones that are damaged before the China time zone to ensure that the China time zone is correct.

/// 2222
Java code 

  1. 1. Public class timetest {
  2. 2. Public static void main (string [] ARGs ){
  3. 3.
  4. 4. // java. util. Calendar. getinstance (). settimezone (timezone. gettimezone ("Asia/Shanghai "));
  5. 5.
  6. 6. // system. Out. println (timezone. getdefault (). GETID ());
  7. 7. system. Out. println (Java. util. Calendar. getinstance (). gettime ());
  8. 8. system. Out. println (Java. util. Calendar. getinstance (). gettimezone ());
  9. 9. system. Out. println (new date (). tolocalestring ());
  10. 10.
  11. 11 .}
  12. 12 .}
1.public class TimeTest {   2.    public static void main(String[] args) {   3.           4.        //java.util.Calendar.getInstance().setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));   5.           6.        //System.out.println(TimeZone.getDefault().getID());   7.        System.out.println(java.util.Calendar.getInstance().getTime());   8.        System.out.println(java.util.Calendar.getInstance().getTimeZone());   9.        System.out.println(new Date().toLocaleString());   10.           11.    }   12.}   

13.

14. The output result is as follows:

15. Mon Mar 17 15:18:24 CST 2008

16. Sun. util. Calendar. zoneinfo [ID = "Asia/Shanghai", offset = 28800000, dstsavings = 0, usedaylight = false, transitions = 19, lastrule = NULL]

15:18:24, 17.2008-3-17

But in Linux, the time zone ID is changed to "America/new_york", and the output is western time.

I found a lot of Chinese materials and did not solve the problem completely. This server used to be "Mon Mar 17 15:18:24 est 2008 ".

", Western time, later changed to CST Shanghai time. The output time in the program is 13 hours different from the system time. Isn't the time zone read by the Java Virtual Machine the time zone set by the system? I don't quite understand what's going on...

Related Article

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.