简体   繁体   中英

How to ensure that java system property `user.timezone` is explicitly specified?

I want to be sure that my Java program is always run by explicitly specified user.timezone property. I pass -Duser.timezone=XXX as a command-line setting.

However, when I omit that system property at all, and then in my program I check the value of System.getProperty("user.timezone") , it is not null , instead, it contains the system timezone. So, I can't terminate my program, as the value of that property is never null.

I know that I can use a custom system property name(say tz ) to accept the timezone ID and then execute TimeZone.setDefault(System.getProperty("tz")) in my code, but I prefer to use the system property user.timezone , which is intended to be used for that reason.

Is there any way to achieve what I need using user.timezone system property?

Specify time zone explicitly as argument to method calls

Relying on the JVM's current default time zone is inherently unreliable. Any code in any thread of any app within the JVM can, at any moment, change the default with a call to TimeZone.setDefault . Such a call immediately affects all other code relying on that default.

Instead, always specify explicitly the time zone you desire/expect.

Also, TimeZone is obsolete, supplanted years ago by the modern java.time classes defined in JSR 310. Specifically, ZoneId and ZoneOffset . You can pass an object of those classes as an optional argument to all the relevant methods.

ZoneId z = ZoneId.of( "Africa/Casablanca" ) ;
ZonedDateTime now = ZonedDateTime.now( z ) ;

UTC is the one true time

On a server, generally the best practice is to set the default time zone of both the host OS and JVM to UTC (an offset-from-UTC of zero).

And most of your business logic, logging, and debugging should all be in UTC as well.

A time zone should only be needed:

  • When localizing display to the user.
  • Where required by a particular business rule.

By the way, that property user.timezone is not listed as one of the standard default properties in Java 11.

I very much agree with the answer by Basil Bourque: Your solution is to write your code so it is independent of a JVM default time zone. Also as mentioned in the comments, when using java.time types with your SQL database through JDBC 4.2 (or later), no default time zone interferes.

Furthermore from what I have read drivers tend to use the database session time zone, not the JVM default time zone. You may want to search your database documentation and your JDBC driver documentation for ways to control the session time zone.

However to answer your question as asked:

    String userTimezoneProp = System.getProperty("user.timezone");
    boolean timezonePropertyEmpty = userTimezoneProp == null || userTimezoneProp.isEmpty();
    if (timezonePropertyEmpty) {
        System.err.println("You must set the user.timezone property to the same time zone as the database time zone.");
        System.exit(-1);
    }

Only you must do this before doing any operations that use the default time zone. Once such an operation is performed, the JVM will query the operating system for a default time zone and set the system property accordingly (if successful).

According to the documentation System.getProperty() should return null for a system property that has not been set. My Java returned an empty string in this example. So I take both possibilities into account. It may be something special for user.timezone . I didn't find any documentation mentioning it. I still do not guarantee that the method is bullet-proof.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM