Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

This article also participated in the “Digitalstar Project” to win a creative gift package and creative incentive money

❤️ Author profile: Java quality creator 🏆, CSDN blog expert certification 🏆, Huawei Cloud enjoy expert certification 🏆

❤️ technology live, the appreciation

❤️ like 👍 collect ⭐ look again, form a habit

scenario

When the JDK is installed on the server, the time zone rules that support the JDK version are provided by default. But time zone rules may change, so how do you keep them up to date?

This section describes the TZUpdater tool

The TZUpdater tool provided allows you to update your installed Java Development Kit (JDK) and Java runtime environment (JRE) software with updated time zone data to accommodate daylight saving Time (DST) changes in different countries. Oracle relies on time zone data exposed through IANA’s Time Zone database.

If you are unable to use the latest JDK or JRE update of Oracle, or if the time zone data on the latest version is not the latest available, the TZUpdater tool provides a way to update the time zone data while leaving other system configurations and dependencies unchanged.

TZUpdater tool usage

TZUpdater JDK/JRE software instance used to execute the tool. The JDK/JRE software is modified with each execution. To manage tools to multiple instances of JDK/JRE software.

Before you can run the TZUpdater tool on your installed JDK/JRE software, you must stop any running services for the JDK/JRE software on your operating system.

Run the TZUpdater tool with the following command:

java -jar tzupdater.jar options
Copy the code

To successfully update the time zone data, you should ensure that you have sufficient permissions to modify the JDK_HOME /jre/lib or JRE_HOME /lib directories.

If no options are specified, a usage message is displayed. To update the time zone data, use the -l or -f option.

options describe
-h, --help
Print the usage tostdoutAnd exit. If this option is specified, other options are ignored.
-V, --version Print the tool version, tzdata version in the JRE, and tzdata version to which the tool will be updated, and exit.
-l, --location url-link-to-archive-file From providingtzdata.tar.gzCompile, test, and update JRE time zone data in the package, for example-l https://www.iana.org/time-zones/repository/tzdata-latest.tar.gz. Supported URL protocols are http://, https://, and file://. If no URL link is provided, the tool will use the latest IANA Tzdata package located therehttps://www.iana.org/time-zones/repository/tzdata-latest.tar.gz.
-f, --force Force tzdata update. Use this option if you update to an older version of TZData.
-v, --verbose Displays detailed messages tostdout.

Manual upgrade

Note:

1. Before running TZUpdater on the installed JDK/JRE software, you must stop any running services of the JDK/JRE software on the operating system.

2. To successfully update the time zone data, you should ensure that you have sufficient permissions to make the changesJDK_HOME /jre/liborJRE_HOME /libDirectory. (In Linux, you must have write permission for the JRE directory. Windows: Run CMD as administrator)

3, If the system has multiple JDK/JRE, you need to use this tool in each JDK/JRE (each JDK/JRE must be operated once).

4, After the update is successful, restart the application service on the JDK/JRE instance (if it has not been updated, try restarting the server)

Operation steps:

1. Download the tzupdater.jar package provided by Oracle. Download address

www.oracle.com/java/techno…

Put tzupdater.jar in the Java directory bin, for example

"C: \ Program Files \ JAVA \ JAVA - 1.8.0 comes with - its - 1.8.0.201 \ bin \ tzupdater jar";Copy the code

2. Check the version of the database in the current time zone. For example, in Windows, run CMD as the administrator to switch to the directory corresponding to tzupdater.jar.

java -jar tzupdater.jar -V
Copy the code

3, online update, take Windows as an example, run CMD as administrator, switch to the corresponding directory of tzupdater.jar :(choose either the third or the fourth update method)

java -jar tzupdater.jar -l https://www.iana.org/time-zones/repository/tzdata-latest.tar.gz
Copy the code

As shown in the figure, tzData2021A has been successfully updated.

The updated file is placed in jre/lib/tzdb.dat, as shown, which has a backup history of the version.

4, offline update: to download the latest time zone data, download address:

Data.iana.org/time-zones/…

In Windows, run CMD as an administrator. Switch to the directory corresponding to tzupdater.jar:

java -jar tzupdater.jar -l file:///[path]/tzdata.tar.gz 
Copy the code

Note:

For Windows, you are advised to save it in the root directory of drive C, and the path directory should not contain Chinese characters.

Run CMD as administrator (write permission required);

As shown in the command above, the/after file is three

5. Run the command in Step 2 to check whether the database version in the current time zone is updated successfully.

Automatic service upgrade

Steps:

1. Set the scheduled task (operating system configuration only) and execute the command script of tzupdater to update the time zone;

2. Create a new time zone service to provide the time zone and DAYLIGHT saving time (DST) rule reading service externally.

3. In the time zone service, write a synchronization button to execute the command script of tzupdater updating the time zone;

4. In the timeZone service, the timeZone data is periodically written to the customized timeZone table. Maintains timeZone data by adding, modifying, and deleting timeZone data.

Advantages of this idea:

1. Other services do not need to stop the service to update the time. The latest time zone data can be obtained by directly invoking the time zone service data.

2, the benefits of automation, avoid manual maintenance of the time zone tedious, manual intervention may cause problems risk;

Time zone services and other business services are split to facilitate future expansion.

Recommend related articles

Hutool date and time series articles

1DateUtil(Time utility class)- current time and current timestamp

2DateUtil(Time tool Class)- Common time types Date, DateTime, Calendar and TemporalAccessor (LocalDateTime) conversions

3DateUtil(Time utility class)- Get various contents for dates

4DateUtil(Time utility class)- Format time

5DateUtil(Time utility class)- Parse the time being formatted

6DateUtil(Time utility class)- Time offset gets

7DateUtil(Time utility class)- Date calculation

8ChineseDate(Lunar Date tools)

9LocalDateTimeUtil({@link LocalDateTime} utility class encapsulation in JDK8+)

10TemporalAccessorUtil{@link TemporalAccessor} Tool class encapsulation

other

To explore the underlying source code at the core of the JDK, you must master native usage

Swastika blog teaches you to understand Java source code date and time related usage

Java’s SimpleDateFormat thread is unsafe

Source code analysis: JDK risks and best practices for getting default time zones

Reference:

TZUpdater: www.oracle.com/java/techno…

IANA Time zone data version: data.iana.org/time-zones/…

JRE time zone data in software version: www.oracle.com/java/techno…