How to Set Java Timezone

How to set a JVM TimeZone Properly

You can pass the JVM this param

-Duser.timezone

For example

-Duser.timezone=Europe/Sofia

and this should do the trick.
Setting the environment variable TZ also does the trick on Linux.

How to set time zone of a java.util.Date?

Use DateFormat. For example,

SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
isoFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = isoFormat.parse("2010-05-23T09:01:02");

how to set timezone for jvm through command line

See your JVM’s current default time zone by calling ZoneId.systemDefault().

You do not explain what is the default time zone of your host OS (which you have not described) versus that of your Vagrant box. I'm not knowledgeable with running Java containerized but I imagine your Java implementation (which you have not yet described) is picking up its default from either the host OS or the container.

Always specify your desired/expected zone

But here is the thing: You should not care. Your Java programming should never rely implicitly on the JVM’s current default time zone. Your Question itself is evidence for my point.

Instead, always specify a time zone explicitly in your code.

Never rely on default zone

Another reason you should never depend on the default is that it can be changed at any moment during runtime(!) by any code in any thread of any app running within the JVM.

Instead pass your desired/expected time zone as an optional argument.

For example, to get the current moment in a zone:

ZoneId z = ZoneId.of( "America/Adak" );
ZonedDateTime zdt = ZonedDateTime( z );

Look throughout the java.time classes and you'll see that wherever a time zone or offset is relevant you can pass a ZoneId or ZoneOffset object as an optional argument. Personally I believe Java programmers would be better off if those arguments were required rather than optional as so many of us fail to think about the issue of time zone and offset.

How to change OS timezone in java?

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.

How do I force a Spring Boot JVM into UTC time zone?

Use spring-boot.run.jvmArguments property if you want to pass JVM options from Maven Spring Boot Plugin to forked Spring Boot application:

<properties>
<spring-boot.run.jvmArguments>-Duser.timezone=UTC</spring-boot.run.jvmArguments>
</properties>

This is be equivalent to command line syntax:

mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Duser.timezone=UTC"

or when running a fully packaged Spring Boot application:

java -Duser.timezone=UTC -jar app.jar

Force Java timezone as GMT/UTC

The OP answered this question to change the default timezone for a single instance of a running JVM, set the user.timezone system property:

java -Duser.timezone=GMT ... <main-class>

If you need to set specific time zones when retrieving Date/Time/Timestamp objects from a database ResultSet, use the second form of the getXXX methods that takes a Calendar object:

Calendar tzCal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
ResultSet rs = ...;
while (rs.next()) {
Date dateValue = rs.getDate("DateColumn", tzCal);
// Other fields and calculations
}

Or, setting the date in a PreparedStatement:

Calendar tzCal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
PreparedStatement ps = conn.createPreparedStatement("update ...");
ps.setDate("DateColumn", dateValue, tzCal);
// Other assignments
ps.executeUpdate();

These will ensure that the value stored in the database is consistent when the database column does not keep timezone information.

The java.util.Date and java.sql.Date classes store the actual time (milliseconds) in UTC. To format these on output to another timezone, use SimpleDateFormat. You can also associate a timezone with the value using a Calendar object:

TimeZone tz = TimeZone.getTimeZone("<local-time-zone>");
//...
Date dateValue = rs.getDate("DateColumn");
Calendar calValue = Calendar.getInstance(tz);
calValue.setTime(dateValue);

Usefull Reference

https://docs.oracle.com/javase/9/troubleshoot/time-zone-settings-jre.htm#JSTGD377

https://confluence.atlassian.com/kb/setting-the-timezone-for-the-java-environment-841187402.html

How do I programmatically set the time zone in Java?

java.util.TimeZone.setDefault() can be used to set a default time zone to be returned by getDefault().

Setting timezone for maven unit tests on Java 8


Short answer

Java now reads user.timezone earlier, before surefire sets the properties in systemPropertyVariables. The solution is to set it earlier, using argLine:

<plugin>
...
<configuration>
<argLine>-Duser.timezone=UTC</argLine>

Long answer

Java initializes the default timezone, taking user.timezone into account the first time it needs it and then caches it in java.util.TimeZone. That now happens already when reading a jar file: ZipFile.getZipEntry now calls ZipUtils.dosToJavaTime which creates a Date instance that initializes the default timezone. This is not a surefire-specific problem. Some call it a bug in JDK7. This program used to print the time in UTC, but now uses the system timezone:

import java.util.*;

class TimeZoneTest {
public static void main(String[] args) {
System.setProperty("user.timezone", "UTC");
System.out.println(new Date());
}
}

In general, the solution is to specify the timezone on the command line, like java -Duser.timezone=UTC TimeZoneTest, or set it programmatically with TimeZone.setDefault(TimeZone.getTimeZone("UTC"));.

Full'ish example:

  <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
... could specify version, other settings if desired ...
<configuration>
<argLine>-Duser.timezone=UTC</argLine>
</configuration>
</plugin>
</plugins>
</build>

How to set the Timezone for iCalendar in Java?

You can use the tz method in the chaining API to set a default output timezone for all your properties.

ICalendar ical = ...
String id = "America/New_York";
TimeZone timezone = TimeZone.getTimeZone(id);
Biweekly.write(ical).tz(timezone, false).go(System.out);


Related Topics



Leave a reply



Submit