4

I have a problem when the current timezone is not determined correctly in a JVM instance running inside a Docker container (both host and container are Cent OS 6.5).

First of all I have mapped etc/localtime to the child container via -v /etc/localtime:/etc/localtime:ro (I think it's a prevalent way)

When I login via SSH both on host and container

date -u

prints Mon Apr 20 11:48:57 UTC 2015

and

date

prints Mon Apr 20 14:50:41 MSK 2015

In JVM however with

System.out.println(new Date());

I get Mon Apr 20 11:52:24 UTC 2015 inside container and Mon Apr 20 14:53:17 MSK 2015 inside the host.

How is the current timezone determined exactly?

Oracle FAQ did not shed the light, I don't quite understand what metrics Java does Java use to get timezone for the current user

Do my operating system's timezone patches fix the Java platform's timezone data?

No. The Java SE platform's timezone data is not read from the local or host operating system. The Java SE platform maintains a private repository of timezone data in locally installed files ( .../jre/lib/zi) as part of the Java Runtime Environment (JRE) software. Applying whatever operating system timezone patches (for example Solaris OS, Linux, Windows) will have no effect on the accuracy of the Java SE platform's timezone data.

update: if someone is interested in workaround - I've specified TZ environment variable as in Stephen's answer, so now the container is created with parameters

-v /etc/localtime:/etc/localtime:ro -e "TZ=Europe/Moscow"

Community
  • 1
  • 1
Boris Treukhov
  • 17,493
  • 9
  • 70
  • 91

1 Answers1

2

According to this page, a JVM running on Linux uses the TZ environment variable to give it the name of the local time zone.

The page goes on to explain that TZ is normally set in "/etc/profile", and it doesn't work if Java is launched using a mechanism that doesn't "source" that file.


The timezone data that the Oracle FAQ refers to is something different. It is the data that the JVM uses to map from timezone names to the corresponding zone offsets (taking account of daylight saving adjustments, etcetera).

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Yes - setting TZ works, however it's mystery why the time is different when TZ is not not set - I'll try to inspect TimeZone.setDefaultZone() in spare time. – Boris Treukhov Apr 20 '15 at 13:06
  • I imagine that TZ is set on the host, and not set in the container, and the JVM behaviour if TZ is unset is to assume UTC. – Stephen C Apr 20 '15 at 13:54