Compare Date objects with different levels of precision

前端 未结 19 1986
日久生厌
日久生厌 2020-12-08 13:01

I have a JUnit test that fails because the milliseconds are different. In this case I don\'t care about the milliseconds. How can I change the precision of the assert to i

19条回答
  •  独厮守ぢ
    2020-12-08 13:26

    You can chose which precision level you want when comparing dates, e.g.:

    LocalDateTime now = LocalDateTime.now().truncatedTo(ChronoUnit.SECONDS);
    // e.g. in MySQL db "timestamp" is without fractional seconds precision (just up to seconds precision)
    assertEquals(myTimestamp, now);
    

提交回复
热议问题