Java Time Zone Update
Hi Folks! We may face consequences that we need to change time zone due to government regulations such as daylight saving time and other purposes. And it is inevitable. For OS side it is very easy to update system time zone. For Linux tzdata package comes for this purpose. But If we have Java applications, updating an OS tzdata package is not enough because Java has its own time zone database. So we need to update Java time zone as well. I confronted last week that, one of Java application(Manage Engine) of the company logged its log file one hour back, even OS time is as expected. Short time later, I noticed that Java time zone is not updated. After some working I have found – In java 1.7 uses zi folder for time zone information whereas, Java 1.8 uses tzdb.dat file. It sounds complicated ? No, Actually does not, Oracle has a tiny tool to update Java time zone. you can download it here . After download, unzip the file. you will have tzupdate.jar file and it has some options you can run it .(Figure-1)
#./jre/bin/java -jar tzupdater.jar Usage: java -jar tzupdater.jar options options: -h, --help Print this message and exit. -V, --version Print tool version, tzdata version in JRE, tzdata version that tool would update to and then exit. -l, --location <URL link to tzdata.tar.gz archive file> Compile, test and update JRE timezone data from provided tzdata.tar.gz bundle e.g -l http://www.iana.org/time-zones/repository/tzdata-latest.tar.gz Supported URL protocols : http://, https://, file:// If no URL link is provided, tool will use latest IANA tzdata bundle at: http://www.iana.org/time-zones/repository/tzdata-latest.tar.gz -f, --force Force a tzdata update. Use this option if updating to an older tzdata version. -v, --verbose Display detailed messages to stdout.
Figure-1 In order to update time zone for Java we need to have something that holds newest time zone information. Actually you can find it. The Internet Assigned Numbers Authority (IANA). Actually, I will not get in more detail what IANA does. You can find more information its official website . For time zone file you can download it here . it is periodically updated by IANA. For package, We chose Data Only Distribution package, in which case we will download tzdata2017a.tar.gz So far so good. TIP.
To download a file on shell you can use wget command.
wget https://www.iana.org/time-zones/repository/releases/tzdata2017a.tar.gz --2017-03-16 07:57:50-- https://www.iana.org/time-zones/repository/releases/tzdata2017a.tar.gz Resolving www.iana.org (www.iana.org)... 18.104.22.168, 2620:0:2d0:200::8 Connecting to www.iana.org (www.iana.org)|22.214.171.124|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 323983 (316K) [application/x-gzip] Saving to: ‘tzdata2017a.tar.gz’ 100%[================================================================================================================================================ 2017-03-16 07:57:52 (306 KB/s) - ‘tzdata2017a.tar.gz’ saved \[323983/323983\]
Update Java Timezone with tzupdater. Do not forget to stop the Java applications before update the time zone. Actually when I first run the command It gave me an error, lucky it is clear.
./jre/bin/java -jar tzupdater.jar -l file:///home/ansible/tzdata2017a.tar.gz Using file:///home/ansible/tzdata2017a.tar.gz as source for tzdata bundle. No SHA-512 hash file found: file:///home/ansible/tzdata2017a.tar.gz.sha512. If using http(s) resource location, the file may not yet be available
To solve the problem follow steps below.
- Take the sha512sum of the tzdate2017a.tar.gz file
#sha512sum tzdata2017a.tar.gz fe57930711998a0755749a2bf1b32fdbc9e6ee249762017beea52eae7e521236396300fe2d79298fd81eb6d73fb7567884c35995ed248be06188329d937e3873 tzdata2017a.tar.gz
- Create a file with the name tzdata2017a.tar.gz.sha512
- Add sha512 hash value of the tzdate2017a.tar.gz to the file tzdata2017a.tar.gz.sha512
#echo fe57930711998a0755749a2bf1b32fdbc9e6ee249762017beea52eae7e521236396300fe2d79298fd81eb6d73fb7567884c35995ed248be06188329d937e3873 > tzdata2017a.tar.gz.sha512
- Try again.
./jre/bin/java -jar tzupdater.jar -l file:///home/ansible/tzdata2017a.tar.gz Using file:///home/ansible/tzdata2017a.tar.gz as source for tzdata bundle.
We got no error. When you look into the folder(/jre/lib/) where Java time zone exists, you can see old time zone was replaced with the newest one.And older one still preserved with different name. zi2014c in this case. Actually, my application has its own bundle(application’s Java folder). So that, I copied it to the other machine and applied time zone update it in that machine due to the production environment issues :). Then copy the zi(newest time zone data) folder the machine, where applications exist. ME/working/jre/lib
drwxr-xr-x. 13 ansible ansible 4096 Mar 16 07:56 zi2014c drwxrwxr-x. 13 ansible ansible 4096 Mar 16 08:00 zi