
org.exparity.hamcrest.date.DateMatchers Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hamcrest-date Show documentation
Show all versions of hamcrest-date Show documentation
Hamcrest Date matchers for Java
package org.exparity.hamcrest.date;
import static java.time.DayOfWeek.*;
import static java.time.Month.*;
import static java.time.ZoneId.systemDefault;
import static java.util.Arrays.asList;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.ZoneId;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;
import java.util.Arrays;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import org.exparity.hamcrest.date.core.IsAfter;
import org.exparity.hamcrest.date.core.IsBefore;
import org.exparity.hamcrest.date.core.IsDayOfMonth;
import org.exparity.hamcrest.date.core.IsDayOfWeek;
import org.exparity.hamcrest.date.core.IsHour;
import org.exparity.hamcrest.date.core.IsLeapYear;
import org.exparity.hamcrest.date.core.IsMaximum;
import org.exparity.hamcrest.date.core.IsMillisecond;
import org.exparity.hamcrest.date.core.IsMinimum;
import org.exparity.hamcrest.date.core.IsMinute;
import org.exparity.hamcrest.date.core.IsMonth;
import org.exparity.hamcrest.date.core.IsSame;
import org.exparity.hamcrest.date.core.IsSameDay;
import org.exparity.hamcrest.date.core.IsSameOrAfter;
import org.exparity.hamcrest.date.core.IsSameOrBefore;
import org.exparity.hamcrest.date.core.IsSecond;
import org.exparity.hamcrest.date.core.IsWithin;
import org.exparity.hamcrest.date.core.IsYear;
import org.exparity.hamcrest.date.core.format.DateFormatter;
import org.exparity.hamcrest.date.core.format.DatePartFormatter;
import org.exparity.hamcrest.date.core.wrapper.DateWrapper;
import org.hamcrest.Matcher;
/**
* Static factory for creating {@link org.hamcrest.Matcher} instances for comparing dates
*
* @author Stewart Bissett
*/
public abstract class DateMatchers {
/**
* Creates a matcher that matches when the examined date is after the reference date
*
* For example:
*
*
* assertThat(myDate, after(new Date()))
*
*
* @param date the reference date against which the examined date is checked
*/
public static Matcher after(final Date date) {
return new IsAfter(new DateWrapper(date), new DateFormatter());
}
/**
* Creates a matcher that matches when the examined date is after the reference date
*
* For example:
*
*
* assertThat(myDate, after(Moments.today()))
*
*
* @param date the reference date against which the examined date is checked
* @deprecated Use {@link #after(LocalDate)}
*/
@Deprecated
public static Matcher after(final DayMonthYear date) {
return after(date.toLocalDate());
}
/**
* Creates a matcher that matches when the examined date is after the reference date
*
* For example:
*
*
* assertThat(myDate, after(Moments.today()))
*
*
* @param date the reference date against which the examined date is checked
*/
public static Matcher after(final LocalDate date) {
return new IsAfter(new DateWrapper(date), new DateFormatter());
}
/**
* Creates a matcher that matches when the examined date is after the end of the reference year
*
* For example:
*
*
* assertThat(myDate, after(2012, Months.MAY, 12));
*
*
* @param year the year against which the examined date is checked
* @param month the month against which the examined date is checked
* @param day the day of the month against which the examined date is checked
* @deprecated Use {@link #after(int, Month, int)}
*/
@Deprecated
public static Matcher after(final int year, final Months month, final int day) {
return new IsAfter(new DateWrapper(year, month.month(), day), new DateFormatter());
}
/**
* Creates a matcher that matches when the examined date is after the end of the reference year
*
* For example:
*
*
* assertThat(myDate, after(2012, Months.MAY, 12));
*
*
* @param year the year against which the examined date is checked
* @param month the month against which the examined date is checked
* @param day the day of the month against which the examined date is checked
*/
public static Matcher after(final int year, final Month month, final int day) {
return new IsAfter(new DateWrapper(year, month, day), new DateFormatter());
}
/**
* Creates a matcher that matches when the examined date is after the end of the reference year
*
* For example:
*
*
* assertThat(myDate, after(2012, Months.MAY, 12, 23, 00, 01));
*
*
* @param year the year against which the examined date is checked
* @param month the month against which the examined date is checked
* @param day the day of the month against which the examined date is checked
* @param hour the hour of the day against which the examined date is checked
* @param minute the minute of the hour against which the examined date is checked
* @param second the second of the minute against which the examined date is checked
* @deprecated Use {@link #after(int, Month, int, int, int, int)}
*/
@Deprecated
public static Matcher after(final int year,
final Months month,
final int dayOfMonth,
final int hour,
final int minute,
final int second) {
return after(year, month.month(), dayOfMonth, hour, minute, second);
}
/**
* Creates a matcher that matches when the examined date is after the end of the reference year
*
* For example:
*
*
* assertThat(myDate, after(2012, Months.MAY, 12, 23, 00, 01));
*
*
* @param year the year against which the examined date is checked
* @param month the month against which the examined date is checked
* @param day the day of the month against which the examined date is checked
* @param hour the hour of the day against which the examined date is checked
* @param minute the minute of the hour against which the examined date is checked
* @param second the second of the minute against which the examined date is checked
*/
public static Matcher after(final int year,
final Month month,
final int dayOfMonth,
final int hour,
final int minute,
final int second) {
return new IsAfter(new DateWrapper(year, month, dayOfMonth, hour, minute, second), new DateFormatter());
}
/**
* Creates a matcher that matches when the examined date is before the reference date
*
* For example:
*
*
* assertThat(myDate, before(new Date()))
*
*
* @param date the reference date against which the examined date is checked
*/
public static Matcher before(final Date date) {
return new IsBefore(new DateWrapper(date), new DateFormatter());
}
/**
* Creates a matcher that matches when the examined date is before the reference date
*
* For example:
*
*
* assertThat(myDate, before(LocalDate.now()))
*
*
* @param date the reference date against which the examined date is checked
*/
public static Matcher before(final LocalDate date) {
return new IsBefore(new DateWrapper(date), new DateFormatter());
}
/**
* Creates a matcher that matches when the examined date is before the reference date
*
* For example:
*
*
* assertThat(myDate, before(Moments.today()));
*
*
* @param date the reference date against which the examined date is checked
* @deprecated Use {@link #before(LocalDate)}
*/
@Deprecated
public static Matcher before(final DayMonthYear date) {
return before(date.toLocalDate());
}
/**
* Creates a matcher that matches when the examined date is before the start of reference day
*
* For example:
*
*
* assertThat(myDate, before(2012, Months.MAY, 12));
*
*
* @param year the year against which the examined date is checked
* @param month the month against which the examined date is checked
* @param dayOfMonth the day of the month against which the examined date is checked
* @deprecated Use {@link #before(int, Month, int)}
*/
@Deprecated
public static Matcher before(final int year, final Months month, final int dayOfMonth) {
return new IsBefore(new DateWrapper(year, month.month(), dayOfMonth), new DateFormatter());
}
/**
* Creates a matcher that matches when the examined date is before the start of reference day
*
* For example:
*
*
* assertThat(myDate, before(2012, Months.MAY, 12));
*
*
* @param year the year against which the examined date is checked
* @param month the month against which the examined date is checked
* @param dayOfMonth the day of the month against which the examined date is checked
*/
public static Matcher before(final int year, final Month month, final int dayOfMonth) {
return new IsBefore(new DateWrapper(year, month, dayOfMonth), new DateFormatter());
}
/**
* Creates a matcher that matches when the examined date is before the start of the reference date and time
*
* For example:
*
*
* assertThat(myDate, before(2012, Months.MAY, 12, 23, 00, 01));
*
*
* @param year the year against which the examined date is checked
* @param month the month against which the examined date is checked
* @param day the day of the month against which the examined date is checked
* @param hour the hour of the day against which the examined date is checked
* @param minute the minute of the hour against which the examined date is checked
* @param second the second of the minute against which the examined date is checked
* @deprecated Use {@link #before(int, Month, int, int, int, int)}
*/
@Deprecated
public static Matcher before(final int year,
final Months month,
final int dayOfMonth,
final int hour,
final int minute,
final int second) {
return before(year, month.month(), dayOfMonth, hour, minute, second);
}
/**
* Creates a matcher that matches when the examined date is before the start of the reference date and time
*
* For example:
*
*
* assertThat(myDate, before(2012, Months.MAY, 12, 23, 00, 01));
*
*
* @param year the year against which the examined date is checked
* @param month the month against which the examined date is checked
* @param day the day of the month against which the examined date is checked
* @param hour the hour of the day against which the examined date is checked
* @param minute the minute of the hour against which the examined date is checked
* @param second the second of the minute against which the examined date is checked
*/
public static Matcher before(final int year,
final Month month,
final int dayOfMonth,
final int hour,
final int minute,
final int second) {
return new IsBefore(new DateWrapper(year, month, dayOfMonth, hour, minute, second), new DateFormatter());
}
/**
* Creates a matcher that matches when the examined date is on the same day of the week as the reference date
*
* For example:
*
*
* assertThat(myDate, sameWeekday(new Date()))
*
*
* @param date the reference date against which the examined date is checked
*/
public static Matcher sameDayOfWeek(final Date date) {
return isDayOfWeek(DayOfWeek.from(date.toInstant().atZone(ZoneId.systemDefault())));
}
/**
* Creates a matcher that matches when the examined date is on the same day of the week as the supplied day
*
* For example:
*
*
* assertThat(myDate, isDayOfWeek(Weekdays.MONDAY))
*
*
* @param weekday the reference weekday against which the examined date is checked
* @deprecated Use {@link #isDayOfWeek(DayOfWeek...)}
*/
@Deprecated
public static Matcher isDayOfWeek(final Weekdays dayOfWeek) {
return new IsDayOfWeek(asList(dayOfWeek.getAsDayOfWeek()), DateMatchers::dateToZoneDateTime);
}
/**
* Creates a matcher that matches when the examined date is on the same day of the week as the supplied day
*
* For example:
*
*
* assertThat(myDate, isDayOfWeek(DayOfWeek.MONDAY))
*
*
* @param weekday the reference weekday against which the examined date is checked
*/
public static Matcher isDayOfWeek(final DayOfWeek... daysOfWeek) {
return new IsDayOfWeek(Arrays.asList(daysOfWeek), DateMatchers::dateToZoneDateTime);
}
/**
* Creates a matcher that matches when the examined date is on the same day of the month as the reference date
*
* For example:
*
*
* assertThat(myDate, sameDayOfMonth(new Date()))
*
*
* @param date the reference date against which the examined date is checked
*/
public static Matcher sameDayOfMonth(final Date date) {
return isDayOfMonth(extractField(date, ChronoField.DAY_OF_MONTH));
}
/**
* Creates a matcher that matches when the examined date is on the expected day of the month
*
* For example:
*
*
* assertThat(myDate, isDayOfMonth(4))
*
*
* @param date the expected day of the month
*/
public static Matcher isDayOfMonth(final int dayOfMonth) {
return new IsDayOfMonth(dayOfMonth, DateMatchers::dateToZoneDateTime);
}
/**
* Creates a matcher that matches when the examined date is on the same day of the year as the reference date
*
* For example:
*
*
* assertThat(myDate, sameDay(new Date()))
*
*
* @param date the reference date against which the examined date is checked
*/
public static Matcher sameDay(final Date date) {
return new IsSameDay(new DateWrapper(date), new DateFormatter());
}
/**
* Creates a matcher that matches when the examined date is on the same day of the year as the reference date
*
* For example:
*
*
* assertThat(myDate, sameDay(Moments.today()));
*
*
* @param date the reference date against which the examined date is checked
* @deprecated Use {@link #sameDay(LocalDate)}
*/
@Deprecated
public static Matcher sameDay(final DayMonthYear date) {
return sameDay(date.toLocalDate());
}
/**
* Creates a matcher that matches when the examined date is on the same day of the year as the reference date
*
* For example:
*
*
* assertThat(myDate, sameDay(Moments.today()));
*
*
* @param date the reference date against which the examined date is checked
*/
public static Matcher sameDay(final LocalDate date) {
return new IsSameDay(new DateWrapper(date), new DateFormatter());
}
/**
* Creates a matcher that matches when the examined date is on the same day of the year as the reference date
*
* For example:
*
*
* assertThat(myDate, sameDayOfTheYear(2012, Month.JAN, 1))
*
*
* @param day the reference day of the month against which the examined date is checked
* @param month the reference month against which the examined date is checked
* @param year the reference year against which the examined date is checked
* @deprecated Use {@link #isDay(int, Month, int)}
*/
@Deprecated
public static Matcher sameDay(final int year, final Months month, final int day) {
return isDay(year, month.month(), day);
}
/**
* Creates a matcher that matches when the examined date is on the same day of the year as the reference date
*
* For example:
*
*
* assertThat(myDate, sameDayOfTheYear(2012, Month.JAN, 1))
*
*
* @param dayOfMonth the reference day of the month against which the examined date is checked
* @param month the reference month against which the examined date is checked
* @param year the reference year against which the examined date is checked
*/
public static Matcher isDay(final int year, final Month month, final int dayOfMonth) {
return sameDay(LocalDate.of(year, month, dayOfMonth));
}
/**
* Creates a matcher that matches when the examined date is on the same hour as the reference date
*
* For example:
*
*
* assertThat(myDate, sameHour(new Date()))
*
*
* @param date the reference date against which the examined date is checked
* @deprecated Use {@link #sameHourOfDay(Date)} instead
*/
@Deprecated
public static Matcher sameHour(final Date date) {
return sameHourOfDay(date);
}
/**
* Creates a matcher that matches when the examined date is on the same hour as the reference date
*
* For example:
*
*
* assertThat(myDate, sameHourOfDay(new Date()))
*
*
* @param date the reference date against which the examined date is checked
*/
public static Matcher sameHourOfDay(final Date date) {
return isHour(extractField(date, ChronoField.HOUR_OF_DAY));
}
/**
* Creates a matcher that matches when the examined date is on the same hour as the reference date
*
* For example:
*
*
* assertThat(myDate, sameHour(12));
*
*
* @param hour the reference hour against which the examined date is checked
* @deprecated Use {@link #isHour(int)} instead
*/
@Deprecated
public static Matcher sameHour(final int hour) {
return isHour(hour);
}
/**
* Creates a matcher that matches when the examined date is on the same hour as the reference date
*
* For example:
*
*
* assertThat(myDate, sameHour(12));
*
*
* @param hour the reference hour against which the examined date is checked
*/
public static Matcher isHour(final int hour) {
return new IsHour(hour, DateMatchers::dateToZoneDateTime);
}
/**
* Creates a matcher that matches when the examined date is on the same instant as the reference date
*
* For example:
*
*
* assertThat(myDate, sameInstant(new Date()))
*
*
* @param date the reference date against which the examined date is checked
*/
public static Matcher sameInstant(final Date date) {
return new IsSame(new DateWrapper(date), new DateFormatter());
}
/**
* Creates a matcher that matches when the examined date is on the same UTC instant as the reference UTC epoch time
* supplied
*
* For example:
*
*
* assertThat(myDate, sameInstant(1325415600000L))
*
*
* @param timestamp the time as milliseconds since the Unix epoch time
*/
public static Matcher sameInstant(final long timestamp) {
return new IsSame(new DateWrapper(new Date(timestamp)), new DateFormatter());
}
/**
* Creates a matcher that matches when the examined date is on the same instance as the reference date
*
* For example:
*
*
* assertThat(myDate, sameInstant(2012, Months.MAY, 12, 23, 00, 01, 123));
*
*
* @param year the year against which the examined date is checked
* @param month the month against which the examined date is checked
* @param day the day of the month against which the examined date is checked
* @param hour the hour of the day against which the examined date is checked
* @param minute the minute of the hour against which the examined date is checked
* @param second the second of the minute against which the examined date is checked
* @param milliseconds the milliseconds of the second against which the examined date is checked
* @deprecated Use {@link #isInstant(int, Month, int, int, int, int, int)}
*/
@Deprecated
public static Matcher sameInstant(final int year,
final Months month,
final int dayOfMonth,
final int hour,
final int minute,
final int second,
final int milliseconds) {
return isInstant(year, month.month(), dayOfMonth, hour, minute, second, milliseconds);
}
/**
* Creates a matcher that matches when the examined date is on the same instance as the reference date
*
* For example:
*
*
* assertThat(myDate, sameInstant(2012, Months.MAY, 12, 23, 00, 01, 123));
*
*
* @param year the year against which the examined date is checked
* @param month the month against which the examined date is checked
* @param day the day of the month against which the examined date is checked
* @param hour the hour of the day against which the examined date is checked
* @param minute the minute of the hour against which the examined date is checked
* @param second the second of the minute against which the examined date is checked
* @param milliseconds the milliseconds of the second against which the examined date is checked
*/
public static Matcher isInstant(final int year,
final Month month,
final int dayOfMonth,
final int hour,
final int minute,
final int second,
final int milliseconds) {
return new IsSame(new DateWrapper(year, month, dayOfMonth, hour, minute, second, milliseconds),
new DateFormatter());
}
/**
* Creates a matcher that matches when the examined date is at the same instant or before the reference date
*
* For example:
*
*
* assertThat(myDate, sameOrBefore(new Date()))
*
*
* @param date the reference date against which the examined date is checked
*/
public static Matcher sameOrBefore(final Date date) {
return new IsSameOrBefore(new DateWrapper(date), new DateFormatter());
}
/**
* Creates a matcher that matches when the examined date is at the same instant or before the reference date
*
* For example:
*
*
* assertThat(myDate, sameOrBefore(LocalDate.now()))
*
*
* @param date the reference date against which the examined date is checked
*/
public static Matcher sameOrBefore(final LocalDate date) {
return new IsSameOrBefore(new DateWrapper(date), new DateFormatter());
}
/**
* Creates a matcher that matches when the examined date is at the same date or before the reference date
*
* For example:
*
*
* assertThat(myDate, sameOrBefore(new Date()))
*
*
* @param date the reference date against which the examined date is checked
* @deprecated Use {@link #sameOrBefore(LocalDate)}
*/
@Deprecated
public static Matcher sameOrBefore(final DayMonthYear date) {
return sameOrBefore(date.toLocalDate());
}
/**
* Creates a matcher that matches when the examined date is on the same day or before the start of the reference
* date
*
* For example:
*
*
* assertThat(myDate, sameOrBefore(2012, Months.MAY, 12));
*
*
* @param year the year against which the examined date is checked
* @param month the month against which the examined date is checked
* @param dayOfMonth the day of the month against which the examined date is checked
* @deprecated Use {@link #sameOrBefore(int, Month, int)}
*/
@Deprecated
public static Matcher sameOrBefore(final int year, final Months month, final int dayOfMonth) {
return sameOrBefore(year, month.month(), dayOfMonth);
}
/**
* Creates a matcher that matches when the examined date is on the same day or before the start of the reference
* date
*
* For example:
*
*
* assertThat(myDate, sameOrBefore(2012, Months.MAY, 12));
*
*
* @param year the year against which the examined date is checked
* @param month the month against which the examined date is checked
* @param dayOfMonth the day of the month against which the examined date is checked
*/
public static Matcher sameOrBefore(final int year, final Month month, final int dayOfMonth) {
return new IsSameOrBefore(new DateWrapper(LocalDate.of(year, month, dayOfMonth)), new DateFormatter());
}
/**
* Creates a matcher that matches when the examined date is on the same second or before the start of the reference
* date and time
*
* For example:
*
*
* assertThat(myDate, sameOrBefore(2012, Months.MAY, 12, 23, 00, 01));
*
*
* @param year the year against which the examined date is checked
* @param month the month against which the examined date is checked
* @param dayOfMonth the day of the month against which the examined date is checked
* @param hour the hour of the day against which the examined date is checked
* @param minute the minute of the hour against which the examined date is checked
* @param second the second of the minute against which the examined date is checked
* @deprecated See {@link #sameOrBefore(int, Month, int, int, int, int)}
*/
@Deprecated
public static Matcher sameOrBefore(final int year,
final Months month,
final int dayOfMonth,
final int hour,
final int minute,
final int second) {
return sameOrBefore(year, month.month(), dayOfMonth, hour, minute, second);
}
/**
* Creates a matcher that matches when the examined date is on the same second or before the start of the reference
* date and time
*
* For example:
*
*
* assertThat(myDate, sameOrBefore(2012, Months.MAY, 12, 23, 00, 01));
*
*
* @param year the year against which the examined date is checked
* @param month the month against which the examined date is checked
* @param dayOfMonth the day of the month against which the examined date is checked
* @param hour the hour of the day against which the examined date is checked
* @param minute the minute of the hour against which the examined date is checked
* @param second the second of the minute against which the examined date is checked
*/
public static Matcher sameOrBefore(final int year,
final Month month,
final int dayOfMonth,
final int hour,
final int minute,
final int second) {
return new IsSameOrBefore(new DateWrapper(year, month, dayOfMonth, hour, minute, second),
new DateFormatter());
}
/**
* F Creates a matcher that matches when the examined date is at the same instant or after the reference date
*
* For example:
*
*
* assertThat(myDate, sameOrAfter(new Date()))
*
*
* @param date the reference date against which the examined date is checked
*/
public static Matcher sameOrAfter(final Date date) {
return new IsSameOrAfter(new DateWrapper(date), new DateFormatter());
}
/**
* F Creates a matcher that matches when the examined date is at the same instant or after the reference date
*
* For example:
*
*
* assertThat(myDate, sameOrAfter(LocalDate.now()))
*
*
* @param date the reference date against which the examined date is checked
*/
public static Matcher sameOrAfter(final LocalDate date) {
return new IsSameOrAfter(new DateWrapper(date), new DateFormatter());
}
/**
* Creates a matcher that matches when the examined date is at the same instant or after the reference date
*
* For example:
*
*
* assertThat(myDate, sameOrAfter(Moments.today()))
*
*
* @param date the reference date against which the examined date is checked
* @deprecated Use {@link #sameOrAfter(LocalDate)}
*/
@Deprecated
public static Matcher sameOrAfter(final DayMonthYear date) {
return sameOrAfter(date.toLocalDate());
}
/**
* Creates a matcher that matches when the examined date is on the same day or after the start of the reference date
*
* For example:
*
*
* assertThat(myDate, sameOrAfter(2012, Months.MAY, 12));
*
*
* @param year the year against which the examined date is checked
* @param month the month against which the examined date is checked
* @param dayOfMonth the day of the month against which the examined date is checked
* @deprecated Use {@link #sameOrAfter(int, Month, int)
*/
@Deprecated
public static Matcher sameOrAfter(final int year, final Months month, final int dayOfMonth) {
return sameOrAfter(year, month.month(), dayOfMonth);
}
/**
* Creates a matcher that matches when the examined date is on the same day or after the start of the reference date
*
* For example:
*
*
* assertThat(myDate, sameOrAfter(2012, Months.MAY, 12));
*
*
* @param year the year against which the examined date is checked
* @param month the month against which the examined date is checked
* @param dayOfMonth the day of the month against which the examined date is checked
*/
public static Matcher sameOrAfter(final int year, final Month month, final int dayOfMonth) {
return new IsSameOrAfter(new DateWrapper(year, month, dayOfMonth), new DateFormatter());
}
/**
* Creates a matcher that matches when the examined date is on the same second or after the start of the reference
* date and time
*
* For example:
*
*
* assertThat(myDate, sameOrAfter(2012, Months.MAY, 12, 23, 00, 01));
*
*
* @param year the year against which the examined date is checked
* @param month the month against which the examined date is checked
* @param day the day of the month against which the examined date is checked
* @param hour the hour of the day against which the examined date is checked
* @param minute the minute of the hour against which the examined date is checked
* @param second the second of the minute against which the examined date is checked
* @deprecated Use {@link #sameOrAfter(int, Month, int, int, int, int)}
*/
@Deprecated
public static Matcher sameOrAfter(final int year,
final Months month,
final int dayOfMonth,
final int hour,
final int minute,
final int second) {
return sameOrAfter(year, month.month(), dayOfMonth, hour, minute, second);
}
/**
* Creates a matcher that matches when the examined date is on the same second or after the start of the reference
* date and time
*
* For example:
*
*
* assertThat(myDate, sameOrAfter(2012, Months.MAY, 12, 23, 00, 01));
*
*
* @param year the year against which the examined date is checked
* @param month the month against which the examined date is checked
* @param day the day of the month against which the examined date is checked
* @param hour the hour of the day against which the examined date is checked
* @param minute the minute of the hour against which the examined date is checked
* @param second the second of the minute against which the examined date is checked
*/
public static Matcher sameOrAfter(final int year,
final Month month,
final int dayOfMonth,
final int hour,
final int minute,
final int second) {
return new IsSameOrAfter(new DateWrapper(year, month, dayOfMonth, hour, minute, second),
new DateFormatter());
}
/**
* Creates a matcher that matches when the examined date is on the same minute as the reference date
*
* For example:
*
*
* assertThat(myDate, sameMinute(new Date()))
*
*
* @param date the reference date against which the examined date is checked
* @deprecated Use {@link #sameMinuteOfHour(Date)} instead
*/
@Deprecated
public static Matcher sameMinute(final Date date) {
return sameMinuteOfHour(date);
}
/**
* Creates a matcher that matches when the examined date is on the same minute as the reference date
*
* For example:
*
*
* assertThat(myDate, sameMinute(new Date()))
*
*
* @param date the reference date against which the examined date is checked
*/
public static Matcher sameMinuteOfHour(final Date date) {
return isMinute(extractField(date, ChronoField.MINUTE_OF_HOUR));
}
/**
* Creates a matcher that matches when the examined date is on the reference minute
*
* For example:
*
*
* assertThat(myDate, sameMinute(55))
*
*
* @param minute the reference minute against which the examined date is checked
* @deprecated Use {@link #isMinute(int)} instead
*/
@Deprecated
public static Matcher sameMinute(final int minute) {
return isMinute(minute);
}
/**
* Creates a matcher that matches when the examined date is on the reference minute
*
* For example:
*
*
* assertThat(myDate, sameMinute(55))
*
*
* @param minute the reference minute against which the examined date is checked
*/
public static Matcher isMinute(final int minute) {
return new IsMinute(minute, DateMatchers::dateToZoneDateTime);
}
/**
* Creates a matcher that matches when the examined date is on the same month as the reference date
*
* For example:
*
*
* assertThat(myDate, sameMonth(new Date()))
*
*
* @param date the reference date against which the examined date is checked
* @deprecated Use {@link #sameMonthOfYear(Date)} instead
*/
@Deprecated
public static Matcher sameMonth(final Date date) {
return sameMonthOfYear(date);
}
/**
* Creates a matcher that matches when the examined date is on the same month as the reference date
*
* For example:
*
*
* assertThat(myDate, sameMonth(new Date()))
*
*
* @param date the reference date against which the examined date is checked
*/
public static Matcher sameMonthOfYear(final Date date) {
return isMonth(Month.of(date.toInstant().atZone(ZoneId.systemDefault()).get(ChronoField.MONTH_OF_YEAR)));
}
/**
* Creates a matcher that matches when the examined date is on the same month as the reference month
*
* For example:
*
*
* assertThat(myDate, sameMonth(Months.DECEMBER))
*
*
* @param month the reference month against which the examined date is checked
* @deprecated Use {@link #sameMonthOfYear(Months)} instead
*/
@Deprecated
public static Matcher sameMonth(final Months month) {
return sameMonthOfYear(month);
}
/**
* Creates a matcher that matches when the examined date is on the same month as the reference month
*
* For example:
*
*
* assertThat(myDate, sameMonth(Months.DECEMBER))
*
*
* @param month the reference month against which the examined date is checked
* @deprecated Use {@link #isMonth(Month)}
*/
@Deprecated
public static Matcher sameMonthOfYear(final Months month) {
return isMonth(month.month());
}
/**
* Creates a matcher that matches when the examined date is on the same second as the reference date
*
* For example:
*
*
* assertThat(myDate, sameSecond(new Date()))
*
*
* @param date the reference date against which the examined date is checked
* @deprecated Use {@link #sameSecondOfMinute(Date)} instead
*/
@Deprecated
public static Matcher sameSecond(final Date date) {
return sameSecondOfMinute(date);
}
/**
* Creates a matcher that matches when the examined date is on the same second as the reference date
*
* For example:
*
*
* assertThat(myDate, sameSecond(new Date()))
*
*
* @param date the reference date against which the examined date is checked
*/
public static Matcher sameSecondOfMinute(final Date date) {
return isSecond(extractField(date, ChronoField.SECOND_OF_MINUTE));
}
/**
* Creates a matcher that matches when the examined date is on the reference second
*
* For example:
*
*
* assertThat(myDate, sameSecond(33))
*
*
* @param second the reference date against which the examined date is checked
* @deprecated Use {@link #isSecond(int)} instead
*/
@Deprecated
public static Matcher sameSecond(final int second) {
return isSecond(second);
}
/**
* Creates a matcher that matches when the examined date is on the reference second
*
* For example:
*
*
* assertThat(myDate, sameSecond(33))
*
*
* @param second the reference date against which the examined date is checked
*/
public static Matcher isSecond(final int second) {
return new IsSecond(second, DateMatchers::dateToZoneDateTime);
}
/**
* Creates a matcher that matches when the examined date is on the same millisecond as the reference date
*
* For example:
*
*
* assertThat(myDate, sameMillisecond(new Date()))
*
*
* @param date the reference date against which the examined date is checked
* @deprecated Use {@link #sameMillisecondOfSecond(Date)} instead
*/
@Deprecated
public static Matcher sameMillisecond(final Date date) {
return sameMillisecondOfSecond(date);
}
/**
* Creates a matcher that matches when the examined date is on the same millisecond as the reference date
*
* For example:
*
*
* assertThat(myDate, sameMillisecond(new Date()))
*
*
* @param date the reference date against which the examined date is checked
*/
public static Matcher sameMillisecondOfSecond(final Date date) {
return isMillisecond(extractField(date, ChronoField.MILLI_OF_SECOND));
}
/**
* Creates a matcher that matches when the examined date is on the reference second
*
* For example:
*
*
* assertThat(myDate, sameMillisecond(123))
*
*
* @param millisecond the millisecond against which the examined date is checked
* @deprecated Use {@link #isMillisecond(int)} instead
*/
@Deprecated
public static Matcher sameMillisecond(final int millisecond) {
return isMillisecond(millisecond);
}
/**
* Creates a matcher that matches when the examined date is on the reference second
*
* For example:
*
*
* assertThat(myDate, sameMillisecond(123))
*
*
* @param millisecond the millisecond against which the examined date is checked
*/
public static Matcher isMillisecond(final int millisecond) {
return new IsMillisecond(millisecond, DateMatchers::dateToZoneDateTime);
}
/**
* Creates a matcher that matches when the examined date is on the same year as the reference date
*
* For example:
*
*
* assertThat(myDate, sameYear(new Date()))
*
*
* @param date the reference date against which the examined date is checked
*/
public static Matcher sameYear(final Date date) {
return isYear(extractField(date, ChronoField.YEAR));
}
/**
* Creates a matcher that matches when the examined date is on the same year as the reference year
*
* For example:
*
*
* assertThat(myDate, sameYear(2013))
*
*
* @param year the reference year against which the examined date is checked
*/
public static Matcher isYear(final int year) {
return new IsYear(year, DateMatchers::dateToZoneDateTime);
}
/**
* Creates a matcher that matches when the examined date is within a defined period the reference date
*
* For example:
*
*
* assertThat(myDate, within(10, TimeUnit.MINUTES, new Date()))
*
*
* @param date the reference date against which the examined date is checked
* @deprecated Use {@link #within(long, ChronoUnit, Date)}
*/
@Deprecated
public static Matcher within(final long period, final TimeUnit unit, final Date date) {
return within(period, convertUnit(unit), date);
}
/**
* Creates a matcher that matches when the examined date is within a defined period the reference date
*
* For example:
*
*
* assertThat(myDate, within(10, TimeUnit.MINUTES, new Date()))
*
*
* @param date the reference date against which the examined date is checked
*/
public static Matcher within(final long period, final ChronoUnit unit, final Date date) {
return new IsWithin(period, unit, new DateWrapper(date), new DateFormatter());
}
/**
* Creates a matcher that matches when the examined date is within a defined period the reference date
*
* For example:
*
*
* assertThat(myDate, within(10, ChronoUnit.DAYS, LocalDate.now()))
*
*
* @param date the reference date against which the examined date is checked
*/
public static Matcher within(final long period, final ChronoUnit unit, final LocalDate date) {
return new IsWithin(period, unit, new DateWrapper(date), new DateFormatter());
}
/**
* Creates a matcher that matches when the examined date is within a defined period the reference date
*
* For example:
*
*
* assertThat(myDate, within(10, TimeUnit.DAYS, Moments.today()))
*
*
* @param date the reference date against which the examined date is checked
* @deprecated Use {@link #within(long, ChronoUnit, LocalDate)}
*/
@Deprecated
public static Matcher within(final long period, final TimeUnit unit, final DayMonthYear date) {
return within(period, convertUnit(unit), date.toLocalDate());
}
/**
* Creates a matcher that matches when the examined date is within a given period of the reference date
*
* For example:
*
*
* assertThat(myDate, within(5, TimeUnit.DAYS, 2012, Months.MAY, 12));
*
*
* @param period the timeunit interval the examined date should be with
* @param unit the timeunit to define the length of the period
* @param year the year against which the examined date is checked
* @param month the month against which the examined date is checked
* @param dayOfMonth the day of the month against which the examined date is checked
* @deprecated Use {@link #within(long, ChronoUnit, int, Month, int)}
*/
@Deprecated
public static Matcher within(final long period,
final TimeUnit unit,
final int year,
final Months month,
final int dayOfMonth) {
return within(period, convertUnit(unit), year, month.month(), dayOfMonth);
}
/**
* Creates a matcher that matches when the examined date is within a given period of the reference date
*
* For example:
*
*
* assertThat(myDate, within(5, TimeUnit.DAYS, 2012, Months.MAY, 12));
*
*
* @param period the timeunit interval the examined date should be with
* @param unit the timeunit to define the length of the period
* @param year the year against which the examined date is checked
* @param month the month against which the examined date is checked
* @param dayOfMonth the day of the month against which the examined date is checked
*/
public static Matcher within(final long period,
final ChronoUnit unit,
final int year,
final Month month,
final int dayOfMonth) {
return within(period, unit, LocalDate.of(year, month, dayOfMonth));
}
/**
* Creates a matcher that matches when the examined date is within a given period of the reference date
*
* For example:
*
*
* assertThat(myDate, within(1, TimeUnit.MINUTES, 2012, Months.MAY, 12, 23, 00, 01));
*
*
* @param period the timeunit interval the examined date should be with
* @param unit the timeunit to define the length of the period
* @param year the year against which the examined date is checked
* @param month the month against which the examined date is checked
* @param day the day of the month against which the examined date is checked
* @param hour the hour of the day against which the examined date is checked
* @param minute the minute of the hour against which the examined date is checked
* @param second the second of the minute against which the examined date is checked
* @param second the millisecond of the second against which the examined date is checked
* @deprecated Use {@link #within(long, ChronoUnit, int, Month, int, int, int, int, int)}
*/
@Deprecated
public static Matcher within(final long period,
final TimeUnit unit,
final int year,
final Months month,
final int dayOfMonth,
final int hour,
final int minute,
final int second,
final int milliseconds) {
return within(period, convertUnit(unit), year, month.month(), dayOfMonth, hour, minute, second, milliseconds);
}
/**
* Creates a matcher that matches when the examined date is within a given period of the reference date
*
* For example:
*
*
* assertThat(myDate, within(1, TimeUnit.MINUTES, 2012, Months.MAY, 12, 23, 00, 01));
*
*
* @param period the timeunit interval the examined date should be with
* @param unit the timeunit to define the length of the period
* @param year the year against which the examined date is checked
* @param month the month against which the examined date is checked
* @param day the day of the month against which the examined date is checked
* @param hour the hour of the day against which the examined date is checked
* @param minute the minute of the hour against which the examined date is checked
* @param second the second of the minute against which the examined date is checked
* @param second the millisecond of the second against which the examined date is checked
*/
public static Matcher within(final long period,
final ChronoUnit unit,
final int year,
final Month month,
final int dayOfMonth,
final int hour,
final int minute,
final int second,
final int milliseconds) {
return new IsWithin(period,
unit,
new DateWrapper(year, month, dayOfMonth, hour, minute, second, milliseconds),
new DateFormatter());
}
/**
* Creates a matcher that matches when the examined date is yesterday
*
* For example:
*
*
* assertThat(myDate, isToday());
*
*/
public static Matcher isYesterday() {
return sameDay(LocalDate.now().minusDays(1));
}
/**
* Creates a matcher that matches when the examined date is today
*
* For example:
*
*
* assertThat(myDate, isToday());
*
*/
public static Matcher isToday() {
return sameDay(LocalDate.now());
}
/**
* Creates a matcher that matches when the examined date is tomorrow
*
* For example:
*
*
* assertThat(myDate, isTomorrow());
*
*/
public static Matcher isTomorrow() {
return sameDay(LocalDate.now().plusDays(1));
}
/**
* Creates a matcher that matches when the examined date is on a monday
*
* For example:
*
*
* assertThat(myDate, isMonday());
*
*/
public static Matcher isMonday() {
return new IsDayOfWeek(DayOfWeek.MONDAY, DateMatchers::dateToZoneDateTime);
}
/**
* Creates a matcher that matches when the examined date is on a tuesday
*
* For example:
*
*
* assertThat(myDate, isTuesday());
*
*/
public static Matcher isTuesday() {
return new IsDayOfWeek(DayOfWeek.TUESDAY, DateMatchers::dateToZoneDateTime);
}
/**
* Creates a matcher that matches when the examined date is on a wednesday
*
* For example:
*
*
* assertThat(myDate, isWednesday());
*
*/
public static Matcher isWednesday() {
return new IsDayOfWeek(DayOfWeek.WEDNESDAY, DateMatchers::dateToZoneDateTime);
}
/**
* Creates a matcher that matches when the examined date is on a thursday
*
* For example:
*
*
* assertThat(myDate, isThursday());
*
*/
public static Matcher isThursday() {
return new IsDayOfWeek(DayOfWeek.THURSDAY, DateMatchers::dateToZoneDateTime);
}
/**
* Creates a matcher that matches when the examined date is on a friday
*
* For example:
*
*
* assertThat(myDate, isFriday());
*
*/
public static Matcher isFriday() {
return new IsDayOfWeek(DayOfWeek.FRIDAY, DateMatchers::dateToZoneDateTime);
}
/**
* Creates a matcher that matches when the examined date is on a saturday
*
* For example:
*
*
* assertThat(myDate, isSaturday());
*
*/
public static Matcher isSaturday() {
return new IsDayOfWeek(DayOfWeek.SATURDAY, DateMatchers::dateToZoneDateTime);
}
/**
* Creates a matcher that matches when the examined date is on a sunday
*
* For example:
*
*
* assertThat(myDate, isSunday());
*
*/
public static Matcher isSunday() {
return new IsDayOfWeek(DayOfWeek.SUNDAY, DateMatchers::dateToZoneDateTime);
}
/**
* Creates a matcher that matches when the examined date is on a weekday
*
* For example:
*
*
* assertThat(myDate, isWeekday());
*
*/
public static Matcher isWeekday() {
return isDayOfWeek(MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY);
}
/**
* Creates a matcher that matches when the examined date is on a weekend
*
* For example:
*
*
* assertThat(myDate, isWeekend());
*
*/
public static Matcher isWeekend() {
return isDayOfWeek(SATURDAY, SUNDAY);
}
/**
* Creates a matcher that matches when the examined date is on the first day of the month
*
* For example:
*
*
* assertThat(myDate, isFirstDayOfMonth());
*
*/
public static Matcher isFirstDayOfMonth() {
return new IsMinimum(ChronoField.DAY_OF_MONTH, DateMatchers::dateToZoneDateTime,
new DatePartFormatter(),
() -> "the date is the first day of the month");
}
/**
* Creates a matcher that matches when the examined date is on the maximum value of the given date part in its
* period
*
* For example:
*
*
* assertThat(myDate, isMaximumDayOfMonth(ChronoField.DAY_OF_MONTH));
*
*
* @param field the temporal field to check
*/
public static Matcher isMinimum(final ChronoField field) {
return new IsMinimum(field, DateMatchers::dateToZoneDateTime, new DatePartFormatter());
}
/**
* Creates a matcher that matches when the examined date is on the first day of the month
*
* For example:
*
*
* assertThat(myDate, isFirstDayOfMonth());
*
*/
public static Matcher isLastDayOfMonth() {
return new IsMaximum(ChronoField.DAY_OF_MONTH, DateMatchers::dateToZoneDateTime,
new DatePartFormatter(),
() -> "the date is the last day of the month");
}
/**
* Creates a matcher that matches when the examined date is on the maximum value of the given date part in its
* period
*
* For example:
*
*
* assertThat(myDate, isMaximum(ChronoField.DAY_OF_MONTH));
*
*
* @param field the temporal field to check
*/
public static Matcher isMaximum(final ChronoField field) {
return new IsMaximum(field, DateMatchers::dateToZoneDateTime, new DatePartFormatter());
}
/**
* Creates a matcher that matches when the examined date is in the expected month
*
* For example:
*
*
* assertThat(myDate, isMonth(Month.AUGUST));
*
*/
public static Matcher isMonth(final Month month) {
return new IsMonth(month, DateMatchers::dateToZoneDateTime);
}
/**
* Creates a matcher that matches when the examined date is in January
*
* For example:
*
*
* assertThat(myDate, isJanuary());
*
*/
public static Matcher isJanuary() {
return isMonth(JANUARY);
}
/**
* Creates a matcher that matches when the examined date is in February
*
* For example:
*
*
* assertThat(myDate, isFebruary());
*
*/
public static Matcher isFebruary() {
return isMonth(FEBRUARY);
}
/**
* Creates a matcher that matches when the examined date is in March
*
* For example:
*
*
* assertThat(myDate, isMarch());
*
*/
public static Matcher isMarch() {
return isMonth(MARCH);
}
/**
* Creates a matcher that matches when the examined date is in April
*
* For example:
*
*
* assertThat(myDate, isApril());
*
*/
public static Matcher isApril() {
return isMonth(APRIL);
}
/**
* Creates a matcher that matches when the examined date is in May
*
* For example:
*
*
* assertThat(myDate, isMay());
*
*/
public static Matcher isMay() {
return isMonth(MAY);
}
/**
* Creates a matcher that matches when the examined date is in June
*
* For example:
*
*
* assertThat(myDate, isJune());
*
*/
public static Matcher isJune() {
return isMonth(JUNE);
}
/**
* Creates a matcher that matches when the examined date is in July
*
* For example:
*
*
* assertThat(myDate, isJuly());
*
*/
public static Matcher isJuly() {
return isMonth(JULY);
}
/**
* Creates a matcher that matches when the examined date is in August
*
* For example:
*
*
* assertThat(myDate, isAugust());
*
*/
public static Matcher isAugust() {
return isMonth(AUGUST);
}
/**
* Creates a matcher that matches when the examined date is in September
*
* For example:
*
*
* assertThat(myDate, isSeptember());
*
*/
public static Matcher isSeptember() {
return isMonth(SEPTEMBER);
}
/**
* Creates a matcher that matches when the examined date is in October
*
* For example:
*
*
* assertThat(myDate, isOctober());
*
*/
public static Matcher isOctober() {
return isMonth(OCTOBER);
}
/**
* Creates a matcher that matches when the examined date is in November
*
* For example:
*
*
* assertThat(myDate, isNovember());
*
*/
public static Matcher isNovember() {
return isMonth(NOVEMBER);
}
/**
* Creates a matcher that matches when the examined date is in December
*
* For example:
*
*
* assertThat(myDate, isDecember());
*
*/
public static Matcher isDecember() {
return isMonth(DECEMBER);
}
/**
* Creates a matcher that matches when the examined date is a leap year
*
* For example:
*
*
* assertThat(myDate, isLeapYear());
*
*/
public static Matcher isLeapYear() {
return new IsLeapYear(DateMatchers::dateToZoneDateTime, new DateFormatter());
}
private static ChronoUnit convertUnit(final TimeUnit unit) {
switch (unit) {
case DAYS:
return ChronoUnit.DAYS;
case HOURS:
return ChronoUnit.HOURS;
case MICROSECONDS:
return ChronoUnit.MICROS;
case MILLISECONDS:
return ChronoUnit.MILLIS;
case MINUTES:
return ChronoUnit.MINUTES;
case NANOSECONDS:
return ChronoUnit.NANOS;
case SECONDS:
return ChronoUnit.SECONDS;
default:
throw new IllegalArgumentException("Unknown TimeUnit '" + unit + "'");
}
}
private static int extractField(final Date date, final ChronoField field) {
return dateToZoneDateTime(date).get(field);
}
private static Temporal dateToZoneDateTime(final Date date) {
return date.toInstant().atZone(systemDefault());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy