Title: Hamcrest Matchers for Java 8 LocalDate
Tags: java;unit-testing;hamcrest
Question: I'd like to test if a java.time.LocalDate is within a fixed number of days of a test date and I'd like to use Hamcrest matchers if possible. Are there any matchers for Hamcrest (java) for working with Dates?


Here is another answer: There is a date matcher extension library for hamcrest, hamcrest-date, which can match LocalDate, LocalDateTime, ZoneDateTime, and Date. To compare if a LocalDate is within a number of days of a test date you can use this syntax:
import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.ChronoUnit;
import org.exparity.hamcrest.date.LocalDateMatchers;
import org.hamcrest.MatcherAssert;
import org.junit.Test;
public class LocalDateTest {
@Test
public void isDate() {
LocalDate actual = LocalDate.now();
LocalDate expected = LocalDate.of(2015, Month.OCTOBER, 15);
MatcherAssert.assertThat(actual,
LocalDateMatchers.within(5, ChronoUnit.DAYS, expected));
The library can be included in you by adding this dependency to your pom.
<dependency&gt;
<groupId&gt;org.exparity</groupId&gt;
<artifactId&gt;hamcrest-date</artifactId&gt;
<version&gt;2.0.1</version&gt;
</dependency&gt;
The project is hosted on github at https://github.com/eXparity/hamcrest-date
Comment for this answer: Can you merge your code into hamcrest ?

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:10/15/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><unit-testing><hamcrest>', 'Title': 'Hamcrest Matchers for Java 8 LocalDate', 'ViewCount': 5461, 'id': 33153537, 'user_AboutMe': None, 'user_AccountId': 1845421.0, 'user_DisplayName': 'stewbis', 'user_Reputation': 350.0, 'user_Views': 39.0, 'user_id': 1673849.0}}