I've added a suite of date matchers which add support for testing if the date is before, after, or within a time period. There are also matchers for testing if a date is the same hour, second, minute, year, etc.
I've added them to the matchers.xml and also setup unit tests, however they use the annotations so aren't invoked by your ant script but if you're happy with the code I can port these over to base off TestCase
When you get some time could you review the changes and consider them for inclusion.
Add date matchers to hamcrest-library
Add date matchers to matchers library and exclude Date helper from test
Remove old copyright notices
There were some old copyright notices hanging around in the code which I've stripped out. Any chance you could check out the code and see if you'd like to pull it into the main hamcrest branch.
I don't see any provision for dealing with time zones. If you are dealing with Calendars, you need to think about time zones. IsSameDayOfMonth is great until your times are 12:30am and 6:30am and your test succeeds when run on the East Coast and fails when run on the West Coast. I've been there and retrofitting these things is not pretty.
One off-the-cuff possibility is to pass a TimeZone along as well, but you might come up with something better if you have some time to spend thinking about it.
Why don't you name your method : hasSameHour, hasSameMinute, etc (instead of isSameHour, isSameMinute,...)
For example : assertThat(date1, hasSameHour(date2)) is more readable as an english sentence than
What do you think ?
I agree that the renamed methods would be better. The project is being developed under https://github.com/modularit/hamcrest-date and has moved on from this original pull request. I'm actively developing the hamcrest-date lib at the other location.
Thanks for your reply.
I have an other comment about the IsSameDayOfTheMonth/IsSameDayOfTheWeek/IsSameDayOfTheYear
It is better to have IsSameDayOfMonth/IsSameDayOfWeek/IsSameDayOfYear (without the extra The) to be consistent with java calendar constants (DAY_OF_MONTH, DAY_OF_WEEK, DAY_OF_YEAR).
What do you think ?
These methods have been deprecated in the latest version of the code. It has been replaced with isSameDatePart() to support any of the Calendar constants. The code is at https://github.com/modularit/hamcrest-date. The hamcrest-date library is also now on the maven central repos if you need to use it. If you'd like changes can I suggest to raise them on the https://github.com/modularit/hamcrest-date project.
Ok, I will do it 👍
@louisth Timezones are system default unless supplied otherwise. Why would 12:30am and 6:30am US East Coast time be the same day, but 12:30am and 6:30am US West Coast time be different days?
This pull request seems to have been closed but these doesn't seem to be any sign of the date package in hamcrest-library. Will these date matchers be part of the standard hamcrest-library distribution going forward or must we get them from the modularit repository?
It will be an add-on library. See the comment above (#6 (comment))