The JVM maintains its own default time zone, unconnected to the host OS time zone settings.
Most every Java implementation defaults on launch to the host OS’ current default time zone. Optionally, you may pass arguments to the JVM launcher to specify another time zone. Lastly, a call to TimeZone.setDefault
immediately changes the current default time zone for all code in all threads of all apps running within that JVM.
I need to change OS timezone from java.
Not possible using the bundled Java classes. No implementation of Java I know of would give that kind of power to a Java app.
You may be able to call some native app or utility from within Java that could alter the host OS time zone settings. But you would have to find such a utility, and set up the bridging call from Java to native code. Or you might be able to execute a shell script to make that OS setting change.
As far as date-time handling within your Java code, you should not rely on the default time zone of either the host OS nor the JVM. I recommend always passing the optional ZoneId
(or ZoneOffset
) to the various date-time methods. Omitting the argument leads to relying implicitly on the JVM's current default time zone. Better to specify your desired/expected time zone.
ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;
By the way, be aware that the Joda-Time project is now in maintenance-mode. Its creator, Stephen Colebourne, went on to create its successor in the java.time classes defined by JSR 310, now bundled with Java 8 and later.