Compare Date objects with different levels of precision

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

    I did a small class that might be useful for some googlers that end up here : https://stackoverflow.com/a/37168645/5930242

    0 讨论(0)
  • 2020-12-08 13:29

    In JUnit you can program two assert methods, like this:

    public class MyTest {
      @Test
      public void test() {
        ...
        assertEqualDates(expectedDateObject, resultDate);
    
        // somewhat more confortable:
        assertEqualDates("01/01/2012", anotherResultDate);
      }
    
      private static final String DATE_PATTERN = "dd/MM/yyyy";
    
      private static void assertEqualDates(String expected, Date value) {
          DateFormat formatter = new SimpleDateFormat(DATE_PATTERN);
          String strValue = formatter.format(value);
          assertEquals(expected, strValue);
      }
    
      private static void assertEqualDates(Date expected, Date value) {
        DateFormat formatter = new SimpleDateFormat(DATE_PATTERN);
        String strExpected = formatter.format(expected);
        String strValue = formatter.format(value);
        assertEquals(strExpected, strValue);
      }
    }
    
    0 讨论(0)
  • 2020-12-08 13:29

    Convert the dates to String using SimpleDateFromat, specify in the constructor the required date/time fields and compare the string values:

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String expectedDate = formatter.format(dateOne));
    String dateToTest = formatter.format(dateTwo);
    assertEquals(expectedDate, dateToTest);
    
    0 讨论(0)
  • 2020-12-08 13:30

    I don't know if there is support in JUnit, but one way to do it:

    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class Example {
    
        private static SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss");
    
        private static boolean assertEqualDates(Date date1, Date date2) {
            String d1 = formatter.format(date1);            
            String d2 = formatter.format(date2);            
            return d1.equals(d2);
        }    
    
        public static void main(String[] args) {
            Date date1 = new Date();
            Date date2 = new Date();
    
            if (assertEqualDates(date1,date2)) { System.out.println("true!"); }
        }
    }
    
    0 讨论(0)
  • 2020-12-08 13:30

    Here is a utility function that did the job for me.

        private boolean isEqual(Date d1, Date d2){
            return d1.toLocalDate().equals(d2.toLocalDate());
        }
    
    
    0 讨论(0)
  • 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.
    
    0 讨论(0)
提交回复
热议问题