Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare Date objects with different levels of precision

Tags:

java

junit

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 ignore milliseconds (or any precision I would like it set to)?

Example of a failing assert that I would like to pass:

Date dateOne = new Date(); dateOne.setTime(61202516585000L); Date dateTwo = new Date(); dateTwo.setTime(61202516585123L); assertEquals(dateOne, dateTwo); 
like image 654
brainimus Avatar asked Nov 04 '09 00:11

brainimus


People also ask

How can I compare two dates?

For comparing the two dates, we have used the compareTo() method. If both dates are equal it prints Both dates are equal. If date1 is greater than date2, it prints Date 1 comes after Date 2. If date1 is smaller than date2, it prints Date 1 comes after Date 2.

How do you compare two dates without the time portion?

If you want to compare just the date part without considering time, you need to use DateFormat class to format the date into some format and then compare their String value. Alternatively, you can use joda-time which provides a class LocalDate, which represents a Date without time, similar to Java 8's LocalDate class.

How can I compare two dates in Java?

In Java, two dates can be compared using the compareTo() method of Comparable interface. This method returns '0' if both the dates are equal, it returns a value "greater than 0" if date1 is after date2 and it returns a value "less than 0" if date1 is before date2.


2 Answers

Yet another workaround, I'd do it like this:

assertTrue("Dates aren't close enough to each other!", (date2.getTime() - date1.getTime()) < 1000); 
like image 126
Esko Avatar answered Oct 17 '22 11:10

Esko


There are libraries that help with this:

Apache commons-lang

If you have Apache commons-lang on your classpath, you can use DateUtils.truncate to truncate the dates to some field.

assertEquals(DateUtils.truncate(date1,Calendar.SECOND),              DateUtils.truncate(date2,Calendar.SECOND)); 

There is a shorthand for this:

assertTrue(DateUtils.truncatedEquals(date1,date2,Calendar.SECOND)); 

Note that 12:00:00.001 and 11:59:00.999 would truncate to different values, so this might not be ideal. For that, there is round:

assertEquals(DateUtils.round(date1,Calendar.SECOND),              DateUtils.round(date2,Calendar.SECOND)); 

AssertJ

Starting with version 3.7.0, AssertJ added an isCloseTo assertions, if you are using the Java 8 Date / Time API.

LocalTime _07_10 = LocalTime.of(7, 10); LocalTime _07_42 = LocalTime.of(7, 42); assertThat(_07_10).isCloseTo(_07_42, within(1, ChronoUnit.HOURS)); assertThat(_07_10).isCloseTo(_07_42, within(32, ChronoUnit.MINUTES)); 

It also works with legacy java Dates as well:

Date d1 = new Date(); Date d2 = new Date(); assertThat(d1).isCloseTo(d2, within(100, ChronoUnit.MILLIS).getValue()); 
like image 36
Dan Watt Avatar answered Oct 17 '22 12:10

Dan Watt