Date date = parseDatetime("2003-04-26T13:01:02"); // 0 ms
Date dateMinusOneMs = new Date(date.getTime() - 1);
// The assertion below fails as seconds fields are different
// even though dates difference is only 1ms
// If we expect a chronological comparison then it should succeed
// If we expect a field by field comparison then it fails as expected
assertThat(date).isInSameSecondAs(dateMinusOneMs);
I suggest to
rename isInSameSecondAs to isEqualToIgnoringMs
add new assertions for chronological comparison with isInSameSecondWindowAs
Date date = parseDatetime("2003-04-26T13:01:02"); // 0 ms
Date dateMinusOneMs = new Date(date.getTime() - 1);
// should fail
assertThat(date).isEqualToIgnoringMilliseconds(dateMinusOneMs);
// should succeed
assertThat(date).isInSameSecondWindowAs(dateMinusOneMs);
Let's see that on an example :
I suggest to
isInSameSecondAs
toisEqualToIgnoringMs
isInSameSecondWindowAs