简体   繁体   中英

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.

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

Here is the Junit Test:

    @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. 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:

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

    return startTime;
}

Instead of assertEquals try 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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