JDK-8249642 : Java display an hour off from system date when TZ env var is set on linux
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util:i18n
  • Affected Version: 8u261
  • Priority: P3
  • Status: Resolved
  • Resolution: Not an Issue
  • Submitted: 2020-07-16
  • Updated: 2020-08-06
  • Resolved: 2020-08-06
Related Reports
Relates :  
Relates :  
Relates :  
Description
Java displays an hour off (+1) from the system date when
TZ environment variable is set to any time zone.

Java displays time correctly as the system time when
there is no TZ variable set in the environment.

On solaris sparc 64bit platform
Java works fine regardless of TZ being set.


Example:

export TZ="EST+5EDT,M3.2.0/2,M11.1.0/2"

and running :

import java.util.Date;

class ExampleDate {
  public static void main(String[] args) {
    Date today = new Date();
    System.out.println(today);
  }
}

gives different result to system date(date)
Comments
The full POSIX TZ syntax for specifying zones is not supported and is an OS-specific behaviour. Please refer to Time-zone IDs section of https://docs.oracle.com/javase/8/docs/api/java/time/ZoneId.html for valid zone options. Prefer using the standard TZDB zone ids in the Continent/Region format. Example : export TZ=America/New_York To default the JVM to New York time zone do: -Duser.timezone=America/New_York This is similar to setting the environment variable TZ: export TZ=America/New_York An alternative is to set a system property from within the application code: System.setProperty("user.timezone", "America/New_York"); or by using TimeZone api from within the application code: TimeZone.setDefault(TimeZone.getTimeZone("America/New_York")); Also refer: https://docs.oracle.com/javase/9/troubleshoot/time-zone-settings-jre.htm#JSTGD359 https://docs.oracle.com/javase/8/docs/api/java/util/TimeZone.html#getDefault-- https://docs.oracle.com/javase/8/docs/api/java/util/TimeZone.html
06-08-2020