Java timezone sheanigans

While running CI tests for a application that is implemented in C and Java, some configuration scripts set the current timezone. The C implemented parts catch the change just nicely, but the java related parts still report the default image timezone.

A simple example:

import java.util.*;
import java.text.*;
class simpleTest
{
        public static void main(String args[])
        {
           Calendar cal = Calendar.getInstance();
           System.out.println("TIME ZONE :"+ cal.getTimeZone().getDisplayName());
        }
}

Result:

vagrant@vm:~$ sudo timedatectl set-timezone America/Aruba 
vagrant@vm:~$ timedatectl 
[..]
                Time zone: America/Aruba (AST, -0400)
[..]
vagrant@vm:~$ java test.java
TIME ZONE :Central European Standard Time
vagrant@vm:~$ ls -alh /etc/localtime 
lrwxrwxrwx 1 root root 35 Jul 10 14:41 /etc/localtime -> ../usr/share/zoneinfo/America/Aruba

It appears the Java implementation uses /etc/timezone as source, not /etc/localtime.

vagrant@vm:~$ echo America/Aruba | sudo tee /etc/timezone 
America/Aruba
vagrant@vm:~$ java test.java
TIME ZONE :Atlantic Standard Time

dpkg-reconfigure tzdata updates this file as well, so using timedatectl only won’t be enough (at least not on Debian based systems which run java based applications.)

Written on July 10, 2023