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.



import java.util.Calendar;
class TimeZoneInfo {
  public static void main(String[] args) {
    Calendar cal = Calendar.getInstance();

Execution result:


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
Java and JavaScript
XXE and Java
Getters and setters (Java)
[Java] Thread and Runnable
Java true and false
[Java] String comparison and && and ||
Java --Serialization and Deserialization
[Java] Arguments and parameters
[Java] Branch and repeat
[Java] Variables and types
java (classes and instances)
[Java] Overload and override
Study Java # 2 (\ mark and operator)
Java version 8 and later features
[Java] Difference between == and equals
[Java] Stack area and static area
[Java] Generics classes and generics methods
Java programming (variables and data)
Java encryption and decryption PDF
Java class definition and instantiation
[Java] About String and StringBuilder
[Java] HashCode and equals overrides
☾ Java / Iterative statement and iterative control statement
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
About Java static and non-static methods
I compared PHP and Java constructors
Use java with MSYS and Cygwin
Distributed tracing with OpenCensus and Java
[Java] Difference between Hashmap and HashTable
Java variable declaration, initialization, and types
Java Excel Insertion and Image Extraction
Install Java and Tomcat with Ansible
AWS SDK for Java 1.11.x and 2.x
[Java] Basic types and instruction notes
Java release date and EOL summary
About fastqc of Biocontainers and Java
Java for beginners, expressions and operators 1
Java Primer Series (Variables and Types)
Encoding and Decoding example in Java
[Java beginner] About abstraction and interface
Basic data types and reference types (Java)
[Java] Loop processing and multiplication table
Java 15 implementation and VS Code preferences
Java arguments, return values and overloads
Java reference mechanism (stack and heap)
Use JDBC with Java and Scala.
Java for beginners, expressions and operators 2
Java PowerPoint password setting and cancellation
Java 9 new features and sample code
[Java beginner] == operator and equals method
[Java] Declare and initialize an array