简体   繁体   English

由于时区不同,时间戳时间不匹配

[英]Timestamp time mismatch because of the different time zone

My team received a new project. 我的团队收到了一个新项目。 The project has a bunch of tests which do not pass. 该项目有一堆没有通过的测试。 By their words, all the tests should pass. 用他们的话说,所有的测试都应该通过。 After investigating the problem I discovered that there is a common issue. 在调查问题后,我发现存在一个共同的问题。 Most of the failing tests create Timestamp and work with it but the expected and the actual date is not the same. 大多数失败的测试创建Timestamp并使用它,但预期和实际日期不一样。 The difference is exactly one hour. 差别只有一个小时。 The old dev team has been working from a zone which is GMT + 1 and we are working from GMT + 2. 旧的开发团队一直在GMT + 1区域工作,我们的工作是GMT + 2。

Why this may happen? 为什么会这样?

EDIT : 编辑

I have a test which creates entity Rating and sets a bunch of properties. 我有一个测试,它创建实体Rating并设置一堆属性。 For example: 例如:

rating.setValidFromDate(new Timestamp(1459841866798L));

then the test assert an expected String constant and rating.toString() 然后测试断言一个预期的String常量和rating.toString()

As I said the difference is exactly 1 hour 正如我所说,差异恰好是1小时

EDIT 2 : 编辑2

@Test
public void testToString() {
    Rating rating = new Rating();
    rating.setValidFromDate(new Timestamp(1459841866798L));

    assertEquals("Rating{validFromDate='2016-04-05 09:37:46'}", rating.toString());
}

@Override
public String toString() {
    StringBuilder builder = new StringBuilder();
    builder.append(Rating.class.getSimpleName());
    builder.append("{");
    builder.append("validFromDate='" + DateHelper.getStringFromDate(validFromDate) + "'");
    builder.append("}");
    return builder.toString();
}

public static String getStringFromDate(Date dateValue) {
    String strDate = "";
    if (dateValue != null) {
        DateFormat dateFormat = new SimpleDateFormat(Constants.DATETIME_FORMAT);
        strDate = dateFormat.format(dateValue);
    }
    return strDate;
}

Solved 解决了

I decided to give you my solution to this problem. 我决定给你解决这个问题的方法。

After reading this: java.sql.Timestamp: changing timezone of Timestamp I found a way to manipulate time zone of the tests. 阅读本文后: java.sql.Timestamp:更改Timestamp的时区我找到了一种操作测试时区的方法。 So I decided to write a JUnit rule which will do this to all of my tests. 所以我决定编写一个JUnit规则来执行所有测试。

I have written the following Statement: 我写了以下声明:

public class GermanTimeZoneStatement extends Statement {

    private final Statement base;

    public GermanTimeZoneStatement(Statement base) {
        super();
        this.base = base;
    }

    @Override
    public void evaluate() throws Throwable {
        TimeZone.setDefault(TimeZone.getTimeZone("Europe/Berlin"));
        base.evaluate();
    }
}

After that I've written this rule: 之后我写了这条规则:

public class GermanTimeZoneRule implements TestRule {

    public static GermanTimeZoneRule getInstance() {
        return new GermanTimeZoneRule();
    }

    @Override
    public Statement apply(Statement base, Description description) {
        return new GermanTimeZoneStatement(base);
    }
}

Finally I've include the rule in my tests like this: 最后,我在我的测试中包含了这样的规则,如下所示:

    @Rule
    public GermanTimeZoneRule germanTimeZoneRule = GermanTimeZoneRule.getInstance();

The code that you're testing is time-zone dependent. 您正在测试的代码与时区有关。 It outputs a particular date according to what time it is in the user's default time-zone . 它根据用户默认时区中的时间输出特定日期。 That's why it gives you different output from what it gives your colleagues. 这就是为什么它为你的同事提供不同的输出。

You need to go back to the specification for that code, and check whether that's the intended behaviour. 您需要返回该代码的规范,并检查这是否是预期的行为。 If it is, you'll need to modify the test to take the time-zone into account. 如果是,则需要修改测试以考虑时区。 If it's not the intended behaviour, then congratulations, your test has exposed a bug! 如果这不是预期的行为,那么恭喜你,你的测试暴露了一个错误!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM