简体   繁体   English

如何在返回类型为 LocalDateTime 的方法中从 LocalDateTime 对象中获取第二个?

[英]How to get the second from LocalDateTime objects in a method with return type LocalDateTime?

I have a problem with getting the seconds from two LocalDateTime objects.我从两个 LocalDateTime 对象中获取秒数时遇到问题。

// a timestammp for the time when the connection was established
public LocalDateTime establish() {

    startTime = LocalDateTime.now();
    connectionEstablished = true;

    return startTime;
}

// timestamp for the time when the connection was disconnected
public LocalDateTime disconnect() {

    endTime = LocalDateTime.now();
    connectionDisconnected = true;

    return endTime;
}
// get the second when the connection was established
public LocalDateTime getStartTime() {

}

// get the seconds when the connection was disconnected
public LocalDateTime getEndTime() {

}

In the method getStartTime() and getEndTime the return type must be from LocalDateTime.在 getStartTime() 和 getEndTime 方法中,返回类型必须来自 LocalDateTime。 I tried different things, for example using Duration.between, using Temporal as return type but it does not work, because the Junit Test gives a error that the return type must be LocalDateTime.我尝试了不同的方法,例如使用 Duration.between,使用 Temporal 作为返回类型但它不起作用,因为 Junit 测试给出返回类型必须为 LocalDateTime 的错误。

Here is the Junit Test:这是 Junit 测试:

    @Test
void test() throws InterruptedException {
    cdr.establish();
    System.out.println(cdr.toString());
    assertTrue(cdr.toString().matches("calling: \\+44 44\\/725 8912, called: \\+1 982\\/543 1201, start: " + TIMESTAMP_PATTERN + ", end: still established"));
    assertEquals(0.0, Duration.between(cdr.getStartTime(), LocalDateTime.now()).getSeconds(), 0.1);
    assertNull(cdr.getEndTime());
    Thread.sleep(1000);
    cdr.establish();
    // it fails here
    assertEquals(1.0, Duration.between(cdr.getStartTime(), LocalDateTime.now()).getSeconds(), 0.1);
    Thread.sleep(1000);
            cdr.disconnect();
    System.out.println(cdr.toString());
    assertTrue(cdr.toString().matches("calling: \\+44 44\\/725 8912, called: \\+1 982\\/543 1201, start: " + TIMESTAMP_PATTERN + ", end: " + TIMESTAMP_PATTERN));
    assertEquals(2.0, Duration.between(cdr.getStartTime(), cdr.getEndTime()).getSeconds(), 0.1);
    Thread.sleep(1000);
    cdr.disconnect();
    assertEquals(2.0, Duration.between(cdr.getStartTime(), cdr.getEndTime()).getSeconds(), 0.1);
}

Any help is appreciated.任何帮助表示赞赏。 Thank you !谢谢 !

You are getting the number of seconds between two local date times correctly.您正在正确获取两个本地日期时间之间的秒数。 The intention of the test is to ensure that when establish is called a second time (ie when the connection is already established), startTime is not reset.测试的目的是确保当第二次调用establish时(即当连接已经建立时), startTime不会被重置。 The test basically says:测试基本上说:

One second later, establish the connection again, the start time should be 1 second before now (, as opposed to exactly now).一秒后,再次建立连接,开始时间应该是现在之前的一秒(而不是正好现在)。

You should probably change the establish method:您可能应该更改establish方法:

public LocalDateTime establish() {
    if (!connectionEstablished) { // add this check
        startTime = LocalDateTime.now();
    }
    connectionEstablished = true;

    return startTime;
}

Instead of assertEquals try assertTrue .而不是assertEquals尝试assertTrue

Here is an example:这是一个例子:

assertTrue(Duration.between(cdr.getStartTime(), LocalDateTime.now()).getSeconds() >= 1L);

Note that getSeconds returns a long value and that 1.0 is not a long value.请注意, getSeconds返回一个long值,而1.0不是long值。

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

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