Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting timezone for maven unit tests on Java 8

How do I set the timezone for unit tests in maven surefire on Java 8?

With Java 7 this used to work with systemPropertyVariables like in the following configuration, but with Java 8 the tests just use the system timezone.

<plugin>   <groupId>org.apache.maven.plugins</groupId>   <artifactId>maven-surefire-plugin</artifactId>   <configuration>     <systemPropertyVariables>       <user.timezone>UTC</user.timezone>     </systemPropertyVariables> 

Why is that, and how do I fix it?

like image 924
Wouter Coekaerts Avatar asked May 05 '14 06:05

Wouter Coekaerts


People also ask

How do I change the default TimeZone in Java?

You can explicitly set a default time zone on the command line by using the Java system property called user. timezone . This bypasses the settings in the Windows operating system and can be a workaround.

What is UTC TimeZone in Java?

UTC stands for Co-ordinated Universal Time. It is time standard and is commonly used across the world. All timezones are computed comparatively with UTC as offset.

How do I get JVM TimeZone?

By default, the JVM reads time zone information from the operating system and stores it in the TimeZone class. To get the default time zone set in the JVM using the method TimeZone. getDefault() . To get the list of all supported timezones, use the method TimeZone.


1 Answers

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> 
like image 159
Wouter Coekaerts Avatar answered Sep 19 '22 19:09

Wouter Coekaerts