Compare Date objects with different levels of precision

前端 未结 19 2079
日久生厌
日久生厌 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:31

    use AssertJ assertions for Joda-Time (http://joel-costigliola.github.io/assertj/assertj-joda-time.html)

    import static org.assertj.jodatime.api.Assertions.assertThat;
    import org.joda.time.DateTime;
    
    assertThat(new DateTime(dateOne.getTime())).isEqualToIgnoringMillis(new DateTime(dateTwo.getTime()));
    

    the test failing message is more readable

    java.lang.AssertionError: 
    Expecting:
      <2014-07-28T08:00:00.000+08:00>
    to have same year, month, day, hour, minute and second as:
      <2014-07-28T08:10:00.000+08:00>
    but had not.
    

提交回复
热议问题