timedatectl and Java TimeZone

Posted in Cho Hisabi. I was so surprised and irritated.

Discovered while working on migrating a Java-built system from Amazon Linux to Amazon Linux 2.

In Amazon Linux 2, execute the following command to set the time zone of the system.

#timedatectl set-timezone "Asia/Tokyo"

Normally, this should be all OK, and even if you search the net, only Amazon Linux 2 and CentOS 7 series articles only mention Cocomade.

However, in the Java program, TimeZone cannot be acquired correctly and becomes UTC.

sample:

TimeZoneInfo.java


import java.util.Calendar;
class TimeZoneInfo {
  public static void main(String[] args) {
    Calendar cal = Calendar.getInstance();
    System.out.println(cal.getTimeZone());
    System.out.println(System.getProperty("user.timezone"));
  }
}

Execution result:

sun.util.calendar.ZoneInfo[id="UTC",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]
UTC

There are several ways to deal with it. (It took a long time to find it)

  1. Set the environment variable TZ = Asia / Tokyo
  2. Add `` `-Duser.timezone = Asia / Tokyo``` to the java command line
  3. Modify `` `/ etc / sysconfig / clock```, which was the standard setting method in the days of Amazon Linux.

etc. I think there are some problems.

--timedatectl will rewrite / etc / localtime, but not `` `/ etc / sysconfig / clock. (It seems that there is no file itself in CentOS 7 in the first place ...) --Java doesn't look at / etc / localtime```. It seems that it depends on the version. It may be a change in the behavior of glibc, but the detailed cause is unknown. --Since it became systemd, / etc / sysconfig is not often referred to, but the legacy of the past remains, so please consider compatibility a little more. .. .. .. --The old system runs on Java 1.6, and the new system runs on Java 7. I don't know what happens with the latest Java 8 or later.

Which is the most correct (or should be) response method? .. .. .. Or rather, standardize the Time Zone settings.

Recommended Posts

timedatectl and Java TimeZone
XXE and Java
[Java] Branch and repeat
[Java] Variables and types
java (classes and instances)
[Java] Overload and override
Java methods and method overloads
java Generics T and? Difference
Advantages and disadvantages of Java
java (conditional branching and repetition)
About Java Packages and imports
[Java] Upload images and base64
C # and Java Overrides Story
Java abstract methods and classes
Java while and for statements
Java encapsulation and getters and setters
OpenJDK 8 java and javac command help
Understanding equals and hashCode in Java
Java generics (defines classes and methods)
BloomFilter description and implementation sample (JAVA)
Enable Java 8 and Java 11 SDKs on Ubuntu
About Java primitive types and reference types
Studying Java 8 (String Joiner and join)
Java
Java basic data types and reference types
Differences between Java and .NET Framework
Java study # 5 (iteration and infinite loop)
[Java] Difference between Closeable and AutoCloseable
Notes on Java path and Package
Install Java 9 on windows 10 and CentOS 7
This and that about Base64 (Java)
Studying java9 (dropWhile, takeWhile and Stream.ofNullable)
Classes and instances Java for beginners
[Java] Difference between StringBuffer and StringBuilder
Encrypt with Java and decrypt with C #
[Java] Difference between length, length () and size ()
I compared Java and Ruby's FizzBuzz.
[Java] Exception types and basic processing
Java adds and reads Excel formulas
Hello world in Java and Gradle
[Java] What are overrides and overloads?
Difference between final and Immutable in Java
java core: Can't reach safepoint and hang? !!
JAVA learning history abstract classes and methods
[Java 7] Divide the Java list and execute the process
Java programming (static clauses and "class variables")
[Java] Proxy for logging SQL and SQL results
[Introduction to Java] Variable declarations and types
Arrylist and linked list difference in java
Program PDF headers and footers in Java
[Java] Difference between Intstream range and rangeClosed
Learn Flyweight patterns and ConcurrentHashMap in Java
[Java8] Search the directory and get the file
[Java] Calculation mechanism, operators and type conversion
Java Direction in C ++ Design and Evolution
Summary of Java Math.random and import (Calendar)
[Java] TreeMap successor method and entrySet complexity
Page number logic and reference code (java)
[Java] Contents of Collection interface and List interface
Java to C and C to Java in Android Studio
Reading and writing gzip files in Java