简答
Java 现在更早地读取 user.timezone,然后再确定 systemPropertyVariables 中的属性.解决方法是提前设置,使用 argLine:
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>
长答案
Java 初始化默认时区,将 user.timezone 考虑到它需要它的 first 时间,然后将其缓存在 java.util.TimeZone代码>.现在在读取 jar 文件时已经发生了这种情况:ZipFile.getZipEntry 现在调用 ZipUtils.dosToJavaTime,它创建了一个初始化默认时区的 Date 实例.这不是一个万无一失的特定问题.有人将其称为 JDK7 中的 bug.该程序用于打印 UTC 时间,但现在使用系统时区:
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());
}
}
一般来说,解决方法是在命令行指定时区,比如java -Duser.timezone=UTC TimeZoneTest,或者用TimeZone.setDefault(TimeZone.getTimeZone编程设置("UTC"));.
Full'ish 示例:
<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>
这篇关于在 Java 8 上为 Maven 单元测试设置时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持前端之家!
本文链接:https://www.f2er.com/3175529.html