栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Java 8上设置Maven单元测试的时区

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Java 8上设置Maven单元测试的时区

简短答案

现在

user.timezone
,在surefire在中设置属性之前,Java会更早阅读
systemPropertyVariables
。解决方案是使用
argLine
以下命令进行更早的设置:

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

长答案

Java会

user.timezone
首次
需要时将默认时区考虑在内,然后将其缓存到中
java.util.TimeZone
。现在,在读取jar文件时已经发生了这种情况:
ZipFile.getZipEntry
现在调用
ZipUtils.dosToJavaTime
创建一个
Date
实例,该实例初始化默认时区。这不是特定于surefire的问题。有人称它为JDK7中的错误。该程序用于以UTC打印时间,但现在使用系统时区:

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

通常,解决方案是在命令行上指定时区,例如

java -Duser.timezone=UTCTimeZoneTest
,或使用编程设置
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

完整示例:

  <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>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/430850.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号