Home  >  Article  >  Java  >  What should I do if the time obtained by Java is inconsistent with the system time?

What should I do if the time obtained by Java is inconsistent with the system time?

coldplay.xixi
coldplay.xixiOriginal
2020-09-02 13:16:594339browse

Solutions to the inconsistency between the time obtained by java and the system time: 1. Add parameters when the JVM is running and specify the time zone; 2. Set the time zone directly in the program; 3. Add 28800 directly.

What should I do if the time obtained by Java is inconsistent with the system time?

Solution to the inconsistency between the java acquisition time and the system time:

Method 1: Add parameters when the JVM is running, Specify time zone -D user.timezone=GMT 08

Method 2: Set the time zone directly in the program. System.setProperty(“user.timezone”,”GMT 08″);

Method 3: Just add 28800 directly (8hours*60min*60sec=28800)

Follow this ideas. Regarding the search-related steps, when tomcat starts, the way to set a default time zone is to add the following line of code in the catalina.sh file

JAVA_OPTS="$JAVA_OPTS -Dfile.encoding=UTF8  -Duser.timezone=GMT+08"

Restart tomcat and it will be normal.

Related learning recommendations: java basic tutorial

The above is the detailed content of What should I do if the time obtained by Java is inconsistent with the system time?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn