Java-acquired system time inconsistency with operating system time

Source: Internet
Author: User
Tags time zones

Problem:

1.new date () Time of the system difference 8 hours

2.eclipse Console printing time differs from system by 8 hours

3.LOG4J log time differs from the system by 8 hours

The above question is actually the same problem.

Reason: The time zone that Java obtains is different from the system time zone

The workaround is as follows:

1. Add the following sentence in front of the code: TimeZone TZ =timezone.gettimezone ("Asia/shanghai"); Timezone.setdefault (TZ)

2. The set java_opts that modifies the Catalina.bat file in Tomcat is followed by the-duser.timezone=gmt+08

3. Change the default time zone in the registry to (gmt+08:00) Beijing, Chongqing, Hong Kong SAR, Urumqi

If not, look down: Export hkey_local_machine\software\microsoft\windowsnt\currentversion\time Zones items from a normal and identical system machine and import them into your own machine.

All three of these methods are solutions.

When you modify the time zone, it is best to use code to see what time zone is available in Java, the code is as follows:

Properties props=system.getproperties ();
?? Iteratoriter=props.keyset (). iterator ();
?? while (Iter.hasnext ())?? stringkey= (String) iter.next ();
?? System.out.println (key+ "=" +props.get (key));
??}

User.timezone=?? is the time zone that Java obtains, and then the operating system's time zone. Go and change the registration form.

Weblogic:

There is a page need to save the date, in the foreground JS debug is correct, but save to the database, found that the date is less than a day, accurate to say is 8 hours earlier than the date of choice, basically can determine is the time zone error,
Start looking, database time zone, no problem, server system time zone, no problem,
The final question actually came out of the WebLogic time zone ...
WebLogic is the use of the JRE, and the default time zone in the JRE is GMT, and China Standard Time everyone knows is gmt+8, the internet has said to change the WebLogic with the JRE in the GMT file, the GMT8 to GMT, but always feel that this is not very good, There is a risk of problems if there is a need to use GMT standard Time
So I started from WebLogic's startup script.
Modify Domians/project/bin/startweblogic.cmd
Find a similar
XML code

Set java_options=%java_options%

Set java_options=%java_options%



The time zone is set to gmt+8 and can also be written as Asia/shanghai after the-duser.timezone=gmt+8 is added.
After modification for
XML code

Set java_options=%java_options%-duser.timezone=gmt+8

Set java_options=%java_options%-duser.timezone=gmt+8


Save, reboot WebLogic, problem solving

File Lookup: Startweblogic.cmd

UNIX Search Command:

Find/-name mk/indicates a lookup directory or a directory to improve efficiency-name means to find by file name

   Set the default time zone:
  timezone tz =timezone.gettimezone ("Asia/shanghai");
   timezone.setdefault (TZ);
  date date=new date ();
  string str=new simpledateformat ("Yyyy-mm-dd hh:mm:ss"). Format (date);
  system.out.println (str);
  
   setting time zone:
  simpledateformat sdf=new simpledateformat ( "yyyy-mm-dd kk:mm:ss ");
  sdf.settimezone (Timezone.gettimezone ("gmt+8"));

Date d = new Date ();
System.out.println (Sdf.format (d));
To set the time zone for a calendar:
Calendar now = Calendar.getinstance ();
Now.settimezone (Timezone.gettimezone ("Asia/shanghai"));//important
System.out.println (Now.get (calendar.year));
System.out.println (Now.get (calendar.month));
System.out.println (Now.get (calendar.day_of_month));
System.out.println (Now.get (calendar.hour_of_day));

To set the time zone in the date format:

SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss", Locale.china);
Formatter.settimezone (Timezone.gettimezone ("gmt+8"));
Date Currtime = new Date ();
String thistime = new String (Formatter.format (currtime));
System.out.println ("gmt+8:" +thistime);
System time Zone:
String Tzid = Timezone.getdefault (). GetID ();
System.out.println ("Timezone.getdefault (). GetID ():" +tzid);

System time Zone:

String zone = System.getproperty ("User.timezone");
System.out.println ("User.timezone:" +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.