Class AbstractDateAssert<SELF extends AbstractDateAssert<SELF>>
- Type Parameters:
SELF- the "self" type of this assertion class. Please read "Emulating 'self types' using Java Generics to simplify fluent API implementation" for more details.
- All Implemented Interfaces:
Assert<SELF,,Date> Descriptable<SELF>,ExtensionPoints<SELF,Date>
- Direct Known Subclasses:
DateAssert
Dates.
Note that assertions with date parameter come with two flavors, one is obviously a Date and the other is a
String representing a Date.
For the latter, the default format follows ISO 8901: "yyyy-MM-dd", user can override it with a custom format by
calling withDateFormat(DateFormat).
The user custom format will then be used for all next Date assertions (i.e not limited to the current assertion) in
the test suite.
To turn back to default format, simply call withDefaultDateFormatsOnly().
- Author:
- Tomasz Nurkiewicz (thanks for giving assertions idea), Joel Costigliola, Mikhail Mazursky, William Delanoue, Michal Kordas, EddĂș MelĂ©ndez
-
Field Summary
Fields inherited from class org.assertj.core.api.AbstractAssert
actual, info, myself, objects, throwUnsupportedExceptionOnEquals -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionhasDayOfMonth(int dayOfMonth) Verifies that the actualDateday of month is equal to the given day of month.hasDayOfWeek(int dayOfWeek) Verifies that the actualDateday of week is equal to the given day of week (seeCalendar.DAY_OF_WEEKfor valid values).hasHourOfDay(int hourOfDay) Verifies that the actualDatehour of day is equal to the given hour of day (24-hour clock).hasMillisecond(int millisecond) Verifies that the actualDatemillisecond is equal to the given millisecond.hasMinute(int minute) Verifies that the actualDateminute is equal to the given minute.hasMonth(int month) Verifies that the actualDatemonth is equal to the given month, month value starting at 1 (January=1, February=2, ...).hasSameTimeAs(String dateAsString) Verifies that the actualDaterepresents the same time as the given date inStringformat.hasSameTimeAs(Date date) hasSecond(int second) Verifies that the actualDatesecond is equal to the given second.hasTime(long timestamp) Verifies that the actualDatehas the same time as the given timestamp.hasYear(int year) Verifies that the actualDateyear is equal to the given year.Same assertion asisAfter(Date)but given date is represented as String either with one of the supported default date formats or a user custom date format (set with methodwithDateFormat(DateFormat)).Verifies that the actualDateis strictly after the givenInstant.Verifies that the actualDateis strictly after the given one.isAfterOrEqualsTo(String dateAsString) Deprecated.isAfterOrEqualsTo(Date other) Deprecated.prefer callingisAfterOrEqualTo(Date)isAfterOrEqualTo(String dateAsString) Same assertion asisAfterOrEqualTo(Date)but given date is represented as String either with one of the supported defaults date format or a user custom date format (set with methodwithDateFormat(DateFormat)).isAfterOrEqualTo(Instant other) Verifies that the actualDateis after or equal to the givenInstant.isAfterOrEqualTo(Date other) Verifies that the actualDateis after or equal to the given one.isAfterYear(int year) Verifies that the actualDateis strictly after the given year.Same assertion asisBefore(Date)but given date is represented as String either with one of the supported default date formats or a user custom date format (set with methodwithDateFormat(DateFormat)).Verifies that the actualDateis strictly before the givenInstant.Verifies that the actualDateis strictly before the given one.isBeforeOrEqualsTo(String dateAsString) Deprecated.prefer callingisBeforeOrEqualTo(String)isBeforeOrEqualsTo(Date other) Deprecated.prefer callingisBeforeOrEqualTo(Date)isBeforeOrEqualTo(String dateAsString) Same assertion asisBeforeOrEqualTo(Date)but given date is represented as String either with one of the supported defaults date format or a user custom date format (set with methodwithDateFormat(DateFormat)).isBeforeOrEqualTo(Instant other) Verifies that the actualDateis before or equal to the givenInstant.isBeforeOrEqualTo(Date other) Verifies that the actualDateis before or equal to the given one.isBeforeYear(int year) Verifies that the actualDateis strictly before the given year.Same assertion asisBetween(Date, Date)but given date is represented as String either with one of the supported defaults date format or a user custom date format (set with methodwithDateFormat(DateFormat)).Same assertion asisBetween(Date, Date, boolean, boolean)but given date is represented as String either with one of the supported default date formats or a user custom date format (set with methodwithDateFormat(DateFormat)).Same assertion asisBetween(Date, Date)but given period is represented withInstant.Same assertion asisBetween(Date, Date, boolean, boolean)but given period is represented withInstant.Verifies that the actualDateis in [start, end[ period (start included, end excluded).Verifies that the actualDateis in the given period defined by start and end dates.
To include start in the period set inclusiveStart parameter totrue.
To include end in the period set inclusiveEnd parameter totrue.Same assertion asisCloseTo(Date, long)but given date is represented as String either with one of the supported defaults date format or a user custom date format (set with methodwithDateFormat(DateFormat)).Verifies that the actualDateis close to the givenInstantby less than delta (expressed in milliseconds), if the difference is equal to delta the assertion succeeds.Verifies that the actualDateis close to the other date by less than delta (expressed in milliseconds), if difference is equal to delta it's ok.Same assertion asisEqualTo(Date date)but given date is represented as aStringeither with one of the supported default date formats or a user custom date format set with methodwithDateFormat(DateFormat).isEqualToIgnoringHours(String dateAsString) Deprecated.UseisCloseTo(Date, long)instead, although not exactly the same semantics, this is the right way to compare with a given precision.isEqualToIgnoringHours(Instant instant) Deprecated.UseisCloseTo(Instant, long)instead, although not exactly the same semantics, this is the right way to compare with a given precision.isEqualToIgnoringHours(Date date) Deprecated.UseisCloseTo(Date, long)instead, although not exactly the same semantics, this is the right way to compare with a given precision.isEqualToIgnoringMillis(String dateAsString) Deprecated.UseisCloseTo(Date, long)instead, although not exactly the same semantics, this is the right way to compare with a given precision.isEqualToIgnoringMillis(Instant instant) Deprecated.UseisCloseTo(Instant, long)instead, although not exactly the same semantics, this is the right way to compare with a given precision.isEqualToIgnoringMillis(Date date) Deprecated.UseisCloseTo(Date, long)instead, although not exactly the same semantics, this is the right way to compare with a given precision.isEqualToIgnoringMinutes(String dateAsString) Deprecated.UseisCloseTo(Date, long)instead, although not exactly the same semantics, this is the right way to compare with a given precision.isEqualToIgnoringMinutes(Instant instant) Deprecated.UseisCloseTo(Instant, long)instead, although not exactly the same semantics, this is the right way to compare with a given precision.isEqualToIgnoringMinutes(Date date) Deprecated.UseisCloseTo(Date, long)instead, although not exactly the same semantics, this is the right way to compare with a given precision.isEqualToIgnoringSeconds(String dateAsString) Deprecated.UseisCloseTo(Date, long)instead, although not exactly the same semantics, this is the right way to compare with a given precision.isEqualToIgnoringSeconds(Instant instant) Deprecated.UseisCloseTo(Instant, long)instead, although not exactly the same semantics, this is the right way to compare with a given precision.isEqualToIgnoringSeconds(Date date) Deprecated.UseisCloseTo(Date, long)instead, although not exactly the same semantics, this is the right way to compare with a given precision.Same assertion asAssert.isIn(Object...)but given dates are represented as String either with one of the supported default date formats or a user custom date format (set with methodwithDateFormat(DateFormat)).Same assertion asAssert.isIn(Object...)but given dates are represented as anjava.time.Instant.isInSameDayAs(String dateAsString) Same assertion asisInSameDayAs(Date)but given date is represented as String either with one of the supported default date formats or a user custom date format (set with methodwithDateFormat(DateFormat)).isInSameDayAs(Instant other) Verifies that actualDateand givenInstanthave the same day of month, month and year fields values.isInSameDayAs(Date other) Verifies that actual and givenDatehave the same day of month, month and year fields values.isInSameHourAs(String dateAsString) Deprecated.UseisCloseTo(Date, long)instead, although not exactly the same semantics, this is the right way to compare with a given precision.isInSameHourAs(Date other) Deprecated.UseisCloseTo(Date, long)instead, although not exactly the same semantics, this is the right way to compare with a given precision.isInSameHourWindowAs(String dateAsString) Same assertion asisInSameHourWindowAs(java.util.Date)but given date is represented as String either with one of the supported default date formats or a user custom date format (set with methodwithDateFormat(DateFormat)).isInSameHourWindowAs(Instant other) Verifies that actualDateand givenInstantare chronologically in the same hour (i.e.isInSameHourWindowAs(Date other) Verifies that actual and givenDateare chronologically in the same hour (i.e.isInSameMinuteAs(String dateAsString) Deprecated.UseisCloseTo(Date, long)instead, although not exactly the same semantics, this is the right way to compare with a given precision.isInSameMinuteAs(Date other) Deprecated.UseisCloseTo(Date, long)instead, although not exactly the same semantics, this is the right way to compare with a given precision.isInSameMinuteWindowAs(String dateAsString) Same assertion asisInSameMinuteWindowAs(Date)but given date is represented as String either with one of the supported default date formats or a user custom date format (set with methodwithDateFormat(DateFormat)).isInSameMinuteWindowAs(Instant other) Verifies that actualDateand givenInstantare chronologically in the same minute (i.e.isInSameMinuteWindowAs(Date other) Verifies that actual and givenDateare chronologically in the same minute (i.e.isInSameMonthAs(String dateAsString) Same assertion asisInSameMonthAs(Date)but given date is represented as String either with one of the supported default date formats or a user custom date format (set with methodwithDateFormat(DateFormat)).isInSameMonthAs(Instant other) Verifies that actualDateand givenInstanthave same month and year fields.isInSameMonthAs(Date other) Verifies that actual and givenDatehave same month and year fields.isInSameSecondAs(String dateAsString) Deprecated.UseisCloseTo(Date, long)instead, although not exactly the same semantics, this is the right way to compare with a given precision.isInSameSecondAs(Date other) Deprecated.UseisCloseTo(Date, long)instead, although not exactly the same semantics, this is the right way to compare with a given precision.isInSameSecondWindowAs(String dateAsString) Same assertion asisInSameSecondWindowAs(Date)but given date is represented as String either with one of the supported default date formats or a user custom date format (set with methodwithDateFormat(DateFormat)).isInSameSecondWindowAs(Instant other) Verifies that actualDateand givenInstantare chronologically strictly in the same second (i.e.isInSameSecondWindowAs(Date other) Verifies that actual and givenDateare chronologically strictly in the same second (i.e.isInSameYearAs(String dateAsString) Same assertion asisInSameYearAs(Date)but given date is represented as String either with one of the supported defaults date format or a user custom date format (set with methodwithDateFormat(DateFormat)).isInSameYearAs(Instant other) Verifies that actualDateand givenInstantare in the same year.isInSameYearAs(Date other) Verifies that actual and givenDateare in the same year.Verifies that the actualDateis strictly in the future.Verifies that the actualDateis strictly in the past.isInWithStringDateCollection(Collection<String> datesAsString) Same assertion asAssert.isIn(Iterable)but given dates are represented as String either with one of the supported defaults date format or a user custom date format (set with methodwithDateFormat(DateFormat)).isNotBetween(String start, String end) Same assertion asisNotBetween(Date, Date)but given date is represented as String either with one of the supported default date formats or a user custom date format (set with methodwithDateFormat(DateFormat)).isNotBetween(String start, String end, boolean inclusiveStart, boolean inclusiveEnd) Same assertion asisNotBetween(Date, Date, boolean, boolean)but given date is represented as String either with one of the supported default date formats or a user custom date format (set with methodwithDateFormat(DateFormat)).isNotBetween(Instant start, Instant end) Verifies that the actualDateis not in [start, end[ periodisNotBetween(Instant start, Instant end, boolean inclusiveStart, boolean inclusiveEnd) Verifies that the actualDateis not in the given period defined by start and end dates.
To include start in the period set inclusiveStart parameter totrue.
To include end in the period set inclusiveEnd parameter totrue.isNotBetween(Date start, Date end) Verifies that the actualDateis not in [start, end[ periodisNotBetween(Date start, Date end, boolean inclusiveStart, boolean inclusiveEnd) Verifies that the actualDateis not in the given period defined by start and end dates.
To include start in the period set inclusiveStart parameter totrue.
To include end in the period set inclusiveEnd parameter totrue.isNotEqualTo(String dateAsString) Same assertion asisNotEqualTo(Date date)but given date is represented as String either with one of the supported default date formats or a user custom date format (set with methodwithDateFormat(DateFormat)).isNotEqualTo(Instant instant) Same assertion asisNotEqualTo(Date date)but given date is represented as anjava.time.Instant.Same assertion asAssert.isNotIn(Object...)but given dates are represented as String either with one of the supported defaults date format or a user custom date format (set with methodwithDateFormat(DateFormat)).Same assertion asAssert.isNotIn(Object...)but given dates are represented asjava.time.Instant.isNotInWithStringDateCollection(Collection<String> datesAsString) Same assertion asAssert.isNotIn(Iterable)but given date is represented as String either with one of the supported default date formats or a user custom date format (set with methodwithDateFormat(DateFormat)).isToday()Verifies that the actualDateis today, that is matching current year, month and day (no check on hour, minute, second, milliseconds).isWithinDayOfMonth(int dayOfMonth) Deprecated.usehasDayOfMonth(int)instead.isWithinDayOfWeek(int dayOfWeek) Deprecated.usehasDayOfWeek(int)instead.isWithinHourOfDay(int hourOfDay) Deprecated.usehasHourOfDay(int)instead.isWithinMillisecond(int millisecond) Deprecated.usehasMillisecond(int)instead.isWithinMinute(int minute) Deprecated.usehasMinute(int)instead.isWithinMonth(int month) Deprecated.usehasMonth(int)instead.isWithinSecond(int second) Deprecated.usehasSecond(int)instead.isWithinYear(int year) Deprecated.usehasYear(int)instead.static voidregisterCustomDateFormat(String userCustomDateFormatPattern) Add the given date format to the ones used to parse date String in String based Date assertions likeisEqualTo(String).static voidregisterCustomDateFormat(DateFormat userCustomDateFormat) Add the given date format to the ones used to parse date String in String based Date assertions likeisEqualTo(String).static voidsetLenientDateParsing(boolean lenientDateParsing) Instead of using default strict date/time parsing, it is possible to use lenient parsing mode for default date formats parser to interpret inputs that do not precisely match supported date formats (lenient parsing).static voidRemove all registered custom date formats => use only the defaults date formats to parse string as date.usingComparator(Comparator<? super Date> customComparator) Use the given custom comparator instead of relying on actual type A equals method for incoming assertion checks.usingComparator(Comparator<? super Date> customComparator, String customComparatorDescription) Use the given custom comparator instead of relying on actual type A equals method for incoming assertion checks.Revert to standard comparison for the incoming assertion checks.withDateFormat(String userCustomDateFormatPattern) Instead of using default date formats for the date String based Date assertions likeisEqualTo(String), AssertJ is going to use any date formats registered with one of these methods : this methodwithDateFormat(java.text.DateFormat)registerCustomDateFormat(java.text.DateFormat)registerCustomDateFormat(String)withDateFormat(DateFormat userCustomDateFormat) Instead of using default date formats for the date String based Date assertions likeisEqualTo(String), AssertJ is going to use any date formats registered with one of these methods :withDateFormat(String)this methodregisterCustomDateFormat(java.text.DateFormat)registerCustomDateFormat(String)Remove all registered custom date formats => use only the default date formats to parse string as date.Methods inherited from class org.assertj.core.api.AbstractAssert
actual, areEqual, asInstanceOf, asList, assertionError, asString, describedAs, descriptionText, doesNotHave, doesNotHaveSameClassAs, doesNotHaveSameHashCodeAs, doesNotHaveToString, doesNotHaveToString, doesNotMatch, doesNotMatch, equals, extracting, extracting, failure, failureWithActualExpected, failWithActualExpectedAndMessage, failWithMessage, getWritableAssertionInfo, has, hashCode, hasSameClassAs, hasSameHashCodeAs, hasToString, hasToString, inBinary, inHexadecimal, is, isElementOfCustomAssert, isEqualTo, isExactlyInstanceOf, isIn, isIn, isInstanceOf, isInstanceOfAny, isInstanceOfSatisfying, isNot, isNotEqualTo, isNotExactlyInstanceOf, isNotIn, isNotIn, isNotInstanceOf, isNotInstanceOfAny, isNotNull, isNotOfAnyClassIn, isNotSameAs, isNull, isOfAnyClassIn, isSameAs, matches, matches, newListAssertInstance, overridingErrorMessage, overridingErrorMessage, satisfies, satisfies, satisfies, satisfiesAnyOf, satisfiesAnyOf, satisfiesAnyOfForProxy, satisfiesForProxy, setCustomRepresentation, setDescriptionConsumer, setPrintAssertionsDescription, throwAssertionError, usingEquals, usingEquals, usingRecursiveAssertion, usingRecursiveAssertion, usingRecursiveComparison, usingRecursiveComparison, withFailMessage, withFailMessage, withRepresentation, withThreadDumpOnErrorMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.assertj.core.api.Descriptable
as, as, as, describedAs, describedAs
-
Constructor Details
-
AbstractDateAssert
-
-
Method Details
-
isEqualTo
Same assertion asisEqualTo(Date date)but given date is represented as aStringeither with one of the supported default date formats or a user custom date format set with methodwithDateFormat(DateFormat).User custom date format takes precedence over the default ones.
Unless specified otherwise, beware that the default formats are expressed in the current local timezone.
Example:
// assertion will pass // theTwoTowers release date : 2002-12-18 assertThat(theTwoTowers.getReleaseDate()).isEqualTo("2002-12-18"); // assertion will fail assertThat(theTwoTowers.getReleaseDate()).isEqualTo("2002-12-19");Defaults date format (expressed in the local time zone unless specified otherwise) are:
yyyy-MM-dd'T'HH:mm:ss.SSSX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ss.SSSyyyy-MM-dd HH:mm:ss.SSSyyyy-MM-dd'T'HH:mm:ssX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ssyyyy-MM-dd
Example of valid string date representations:
2003-04-26T03:01:02.758+00:002003-04-26T03:01:02.9992003-04-26 03:01:02.9992003-04-26T03:01:02+00:002003-04-26T13:01:022003-04-26
If you are getting an
IllegalArgumentExceptionwith "Unknown pattern character 'X'" message (some Android versions don't support it), you can explicitly specify the date format to use so that the default ones are bypassed.- Parameters:
dateAsString- the given Date represented as String in default or custom date format.- Returns:
- this assertion object.
- Throws:
AssertionError- if actual and given Date represented as String are not equal.AssertionError- if the given date as String could not be converted to a Date.
-
isEqualTo
CallsisEqualTo(Date date)after converting the givenInstantto aDate.Example:
// theTwoTowers release date : 2002-12-18 assertThat(theTwoTowers.getReleaseDate()).isEqualTo(Instant.parse("2002-12-18T00:00:00.00Z"));- Parameters:
instant- the givenInstantto compare to actual.- Returns:
- this assertion object.
- Throws:
NullPointerException- if givenInstantisnull.AssertionError- if actualDateand givenInstantare not equal (after converting instant to a Date).
-
isEqualToIgnoringHours
Deprecated.UseisCloseTo(Date, long)instead, although not exactly the same semantics, this is the right way to compare with a given precision.Same assertion asisEqualToIgnoringHours(Date)but given Date is represented as String either with one of the default supported date formats or user custom date format (set with methodwithDateFormat(DateFormat)).User custom date format take precedence over the default ones.
Unless specified otherwise, beware that the default formats are expressed in the current local timezone.
Example:
// OK : all dates fields are the same up to minutes excluded assertThat("2003-04-26T13:01:35").isEqualToIgnoringHours("2003-04-26T14:02:35"); // KO : fail as day fields differ assertThat("2003-04-26T14:01:35").isEqualToIgnoringHours("2003-04-27T13:02:35")Defaults date format (expressed in the local time zone unless specified otherwise) are:
yyyy-MM-dd'T'HH:mm:ss.SSSX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ss.SSSyyyy-MM-dd HH:mm:ss.SSSyyyy-MM-dd'T'HH:mm:ssX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ssyyyy-MM-dd
Example of valid string date representations:
2003-04-26T03:01:02.758+00:002003-04-26T03:01:02.9992003-04-26 03:01:02.9992003-04-26T03:01:02+00:002003-04-26T13:01:022003-04-26
If you are getting an
IllegalArgumentExceptionwith "Unknown pattern character 'X'" message (some Android versions don't support it), you can explicitly specify the date format to use so that the default ones are bypassed.- Parameters:
dateAsString- the given Date represented as String in default or custom date format.- Returns:
- this assertion object.
- Throws:
AssertionError- if actual and given Date represented as String are not equal ignoring hours, minutes, seconds and milliseconds.AssertionError- if the given date as String could not be converted to a Date.
-
isEqualToIgnoringHours
Deprecated.UseisCloseTo(Instant, long)instead, although not exactly the same semantics, this is the right way to compare with a given precision.Same assertion asisEqualToIgnoringHours(Date)but given Date is represented as anjava.time.Instant.Example:
assertThat(new Date()).isEqualToIgnoringHours(Instant.now());- Parameters:
instant- the givenInstant.- Returns:
- this assertion object.
- Throws:
AssertionError- if actualDateand givenInstantare not equal ignoring hours, minutes, seconds and milliseconds.- Since:
- 3.19.0
-
isEqualToIgnoringHours
Deprecated.UseisCloseTo(Date, long)instead, although not exactly the same semantics, this is the right way to compare with a given precision.Same assertion asAbstractAssert.isEqualTo(Object)but the comparison ignores hours, minutes, seconds and milliseconds.Example:
Date date1 = parseDatetime("2003-04-26T13:01:35"); Date date2 = parseDatetime("2003-04-26T14:01:00"); Date date3 = parseDatetime("2003-04-27T13:01:35"); // OK : all dates fields are the same up to hours excluded assertThat(date1).isEqualToIgnoringHours(date2); // KO : fail as day fields differ assertThat(date1).isEqualToIgnoringHours(date3);- Parameters:
date- the given Date.- Returns:
- this assertion object.
- Throws:
AssertionError- if actual and given Date represented as String are not equal ignoring hours, minutes, seconds and milliseconds.AssertionError- if the given date as String could not be converted to a Date.
-
isEqualToIgnoringMinutes
Deprecated.UseisCloseTo(Date, long)instead, although not exactly the same semantics, this is the right way to compare with a given precision.Same assertion asisEqualToIgnoringMinutes(Date)but given Date is represented as String either with one of the default supported date format or user custom date format (set with methodwithDateFormat(DateFormat)).User custom date format take precedence over the default ones.
Unless specified otherwise, beware that the default formats are expressed in the current local timezone.
Example:
withDateFormat("yyyy-MM-dd'T'HH:mm:ss"); // OK : all dates fields are the same up to minutes excluded assertThat("2003-04-26T13:01:35").isEqualToIgnoringMinutes("2003-04-26T13:02:35"); // KO : fail as hour fields differ assertThat("2003-04-26T14:01:35").isEqualToIgnoringMinutes("2003-04-26T13:02:35")Defaults date format (expressed in the local time zone unless specified otherwise) are:
yyyy-MM-dd'T'HH:mm:ss.SSSX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ss.SSSyyyy-MM-dd HH:mm:ss.SSSyyyy-MM-dd'T'HH:mm:ssX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ssyyyy-MM-dd
Example of valid string date representations:
2003-04-26T03:01:02.758+00:002003-04-26T03:01:02.9992003-04-26 03:01:02.9992003-04-26T03:01:02+00:002003-04-26T13:01:022003-04-26
If you are getting an
IllegalArgumentExceptionwith "Unknown pattern character 'X'" message (some Android versions don't support it), you can explicitly specify the date format to use so that the default ones are bypassed.- Parameters:
dateAsString- the given Date represented as String in default or custom date format.- Returns:
- this assertion object.
- Throws:
AssertionError- if actual and given Date represented as String are not equal ignoring minutes, seconds and milliseconds.AssertionError- if the given date as String could not be converted to a Date.
-
isEqualToIgnoringMinutes
Deprecated.UseisCloseTo(Instant, long)instead, although not exactly the same semantics, this is the right way to compare with a given precision.Same assertion asisEqualToIgnoringMinutes(Date)but given Date is represented as anjava.time.Instant.Example:
assertThat(new Date()).isEqualToIgnoringMinutes(Instant.now());- Parameters:
instant- the givenInstant.- Returns:
- this assertion object.
- Throws:
AssertionError- if actualDateand givenInstantare not equal ignoring minutes, seconds and milliseconds.- Since:
- 3.19.0
-
isEqualToIgnoringMinutes
Deprecated.UseisCloseTo(Date, long)instead, although not exactly the same semantics, this is the right way to compare with a given precision.Same assertion asAbstractAssert.isEqualTo(Object)but given Date should not take care of minutes, seconds and millisecond precision.Example:
Date date1 = parseDatetime("2003-04-26T13:01:35"); Date date2 = parseDatetime("2003-04-26T13:02:00"); Date date3 = parseDatetime("2003-04-26T14:02:00"); // OK : all dates fields are the same up to minutes excluded assertThat(date1).isEqualToIgnoringMinutes(date2); // KO : fail as hour fields differ assertThat(date1).isEqualToIgnoringMinutes(date3);- Parameters:
date- the given Date.- Returns:
- this assertion object.
- Throws:
AssertionError- if actual and given Date represented as String are not equal ignoring minutes, seconds and milliseconds.AssertionError- if the given date as String could not be converted to a Date.
-
isEqualToIgnoringSeconds
Deprecated.UseisCloseTo(Date, long)instead, although not exactly the same semantics, this is the right way to compare with a given precision.Same assertion asisEqualToIgnoringSeconds(Date)but given Date is represented as String either with one of the default supported date formats or user custom date format (set with methodwithDateFormat(DateFormat)).User custom date format takes precedence over the default ones.
Unless specified otherwise, beware that the default formats are expressed in the current local timezone.
Example:
Date date1 = parseDatetime("2003-04-26T13:01:35"); // OK : all dates fields are the same up to seconds excluded assertThat(date1).isEqualToIgnoringSeconds("2003-04-26T13:01:57"); // KO : fail as minute fields differ assertThat(date1).isEqualToIgnoringMinutes("2003-04-26T13:02:00")Defaults date format (expressed in the local time zone unless specified otherwise) are:
yyyy-MM-dd'T'HH:mm:ss.SSSX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ss.SSSyyyy-MM-dd HH:mm:ss.SSSyyyy-MM-dd'T'HH:mm:ssX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ssyyyy-MM-dd
Example of valid string date representations:
2003-04-26T03:01:02.758+00:002003-04-26T03:01:02.9992003-04-26 03:01:02.9992003-04-26T03:01:02+00:002003-04-26T13:01:022003-04-26
If you are getting an
IllegalArgumentExceptionwith "Unknown pattern character 'X'" message (some Android versions don't support it), you can explicitly specify the date format to use so that the default ones are bypassed.- Parameters:
dateAsString- the given Date represented as String in default or custom date format.- Returns:
- this assertion object.
- Throws:
AssertionError- if actual and given Date represented as String are not equal ignoring seconds and milliseconds.AssertionError- if the given date as String could not be converted to a Date.
-
isEqualToIgnoringSeconds
Deprecated.UseisCloseTo(Instant, long)instead, although not exactly the same semantics, this is the right way to compare with a given precision.Same assertion asisEqualToIgnoringSeconds(Date)but given Date is represented as anjava.time.Instant.Example:
assertThat(new Date()).isEqualToIgnoringSeconds(Instant.now());- Parameters:
instant- the givenInstant.- Returns:
- this assertion object.
- Throws:
AssertionError- if actualDateand givenInstantare not equal ignoring seconds and milliseconds.- Since:
- 3.19.0
-
isEqualToIgnoringSeconds
Deprecated.UseisCloseTo(Date, long)instead, although not exactly the same semantics, this is the right way to compare with a given precision.Same assertion asAbstractAssert.isEqualTo(Object)but given Date should not take care of seconds and millisecond precision.Example:
Date date1 = parseDatetime("2003-04-26T13:01:35"); Date date2 = parseDatetime("2003-04-26T13:01:36"); // OK : all dates fields are the same up to seconds excluded assertThat(date1).isEqualToIgnoringSeconds(date2); // KO : fail as minute fields differ Date date3 = parseDatetime("2003-04-26T13:02:00"); assertThat(date1).isEqualToIgnoringSeconds(date3);- Parameters:
date- the given Date represented as String in default or custom date format.- Returns:
- this assertion object.
- Throws:
AssertionError- if actual and given Date represented as String are not equal ignoring seconds and milliseconds.AssertionError- if the given date as String could not be converted to a Date.
-
isEqualToIgnoringMillis
Deprecated.UseisCloseTo(Date, long)instead, although not exactly the same semantics, this is the right way to compare with a given precision.Same assertion asisEqualToIgnoringMillis(Date)but given Date is represented as String either with one of the default supported date formats or user custom date format (set with methodwithDateFormat(DateFormat)).User custom date format takes precedence over the default ones.
Unless specified otherwise, beware that the default formats are expressed in the current local timezone.
Example:
Date date1 = parseDatetimeWithMs("2003-04-26T13:01:35.998"); // OK : all dates fields are the same up to milliseconds excluded assertThat().isEqualToIgnoringMillis("2003-04-26T13:01:35.997"); // KO : fail as seconds fields differ assertThat("2003-04-26T13:01:35.998").isEqualToIgnoringMinutes("2003-04-26T13:01:36.998")Defaults date format (expressed in the local time zone unless specified otherwise) are:
yyyy-MM-dd'T'HH:mm:ss.SSSX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ss.SSSyyyy-MM-dd HH:mm:ss.SSSyyyy-MM-dd'T'HH:mm:ssX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ssyyyy-MM-dd
Example of valid string date representations:
2003-04-26T03:01:02.758+00:002003-04-26T03:01:02.9992003-04-26 03:01:02.9992003-04-26T03:01:02+00:002003-04-26T13:01:022003-04-26
If you are getting an
IllegalArgumentExceptionwith "Unknown pattern character 'X'" message (some Android versions don't support it), you can explicitly specify the date format to use so that the default ones are bypassed.- Parameters:
dateAsString- the given Date represented as String in default or custom date format.- Returns:
- this assertion object.
- Throws:
AssertionError- if actual and given Date represented as String are not equal ignoring milliseconds.AssertionError- if the given date as String could not be converted to a Date.
-
isEqualToIgnoringMillis
Deprecated.UseisCloseTo(Instant, long)instead, although not exactly the same semantics, this is the right way to compare with a given precision.Same assertion asisEqualToIgnoringMillis(Date)but given Date is represented as anjava.time.Instant.Example:
assertThat(new Date()).isEqualToIgnoringMillis(Instant.now());- Parameters:
instant- the givenInstant.- Returns:
- this assertion object.
- Throws:
AssertionError- if actualDateand givenInstantare not equal ignoring milliseconds.- Since:
- 3.19.0
-
isEqualToIgnoringMillis
Deprecated.UseisCloseTo(Date, long)instead, although not exactly the same semantics, this is the right way to compare with a given precision.Same assertion asAbstractAssert.isEqualTo(Object)but given Date should not take care of milliseconds precision.Example:
Date date1 = parseDatetimeAndMs("2003-04-26T13:01:35.001"); Date date2 = parseDatetimeAndMs("2003-04-26T13:01:35.002"); Date date3 = parseDatetimeAndMs("2003-04-26T14:01:36.001"); // OK : all dates fields are the same up to milliseconds excluded assertThat(date1).isEqualToIgnoringMillis(date2); // KO : fail as second fields differ assertThat(date1).isEqualToIgnoringMillis(date3);- Parameters:
date- the given Date represented as String in default or custom date format.- Returns:
- this assertion object.
- Throws:
AssertionError- if actual and given Date represented as String are not equal ignoring milliseconds.AssertionError- if the given date as String could not be converted to a Date.
-
isNotEqualTo
Same assertion asisNotEqualTo(Date date)but given date is represented as String either with one of the supported default date formats or a user custom date format (set with methodwithDateFormat(DateFormat)).User custom date format takes precedence over the default ones.
Unless specified otherwise, beware that the default formats are expressed in the current local timezone.
Example:
// assertion will pass // theTwoTowers release date : 2002-12-18 assertThat(theTwoTowers.getReleaseDate()).isNotEqualTo("2002-12-19"); // assertion will fail assertThat(theTwoTowers.getReleaseDate()).isNotEqualTo("2002-12-18")Defaults date format (expressed in the local time zone unless specified otherwise) are:
yyyy-MM-dd'T'HH:mm:ss.SSSX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ss.SSSyyyy-MM-dd HH:mm:ss.SSSyyyy-MM-dd'T'HH:mm:ssX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ssyyyy-MM-dd
Example of valid string date representations:
2003-04-26T03:01:02.758+00:002003-04-26T03:01:02.9992003-04-26 03:01:02.9992003-04-26T03:01:02+00:002003-04-26T13:01:022003-04-26
If you are getting an
IllegalArgumentExceptionwith "Unknown pattern character 'X'" message (some Android versions don't support it), you can explicitly specify the date format to use so that the default ones are bypassed.- Parameters:
dateAsString- the given Date represented as String in default or custom date format.- Returns:
- this assertion object.
- Throws:
AssertionError- if actual and given Date represented as String are equal.AssertionError- if the given date as String could not be converted to a Date.
-
isNotEqualTo
Same assertion asisNotEqualTo(Date date)but given date is represented as anjava.time.Instant.Example:
// assertion will pass // theTwoTowers release date : 2002-12-18 assertThat(theTwoTowers.getReleaseDate()).isNotEqualTo(Instant.now());- Parameters:
instant- the givenInstant.- Returns:
- this assertion object.
- Throws:
AssertionError- if actualDateand givenInstantare equal.- Since:
- 3.19.0
-
isIn
Same assertion asAssert.isIn(Object...)but given dates are represented as String either with one of the supported default date formats or a user custom date format (set with methodwithDateFormat(DateFormat)).User custom date format takes precedence over the default ones.
Unless specified otherwise, beware that the default formats are expressed in the current local timezone.
Example:
// assertion will pass // theTwoTowers release date : 2002-12-18 assertThat(theTwoTowers.getReleaseDate()).isIn("2002-12-17", "2002-12-18", "2002-12-19"); // assertion will fail assertThat(theTwoTowers.getReleaseDate()).isIn("2002-12-17", "2002-12-19", "2002-12-20")Defaults date format (expressed in the local time zone unless specified otherwise) are:
yyyy-MM-dd'T'HH:mm:ss.SSSX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ss.SSSyyyy-MM-dd HH:mm:ss.SSSyyyy-MM-dd'T'HH:mm:ssX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ssyyyy-MM-dd
Example of valid string date representations:
2003-04-26T03:01:02.758+00:002003-04-26T03:01:02.9992003-04-26 03:01:02.9992003-04-26T03:01:02+00:002003-04-26T13:01:022003-04-26
If you are getting an
IllegalArgumentExceptionwith "Unknown pattern character 'X'" message (some Android versions don't support it), you can explicitly specify the date format to use so that the default ones are bypassed.- Parameters:
datesAsString- the given Dates represented as String in default or custom date format.- Returns:
- this assertion object.
- Throws:
AssertionError- if actual is not in given Dates represented as String.AssertionError- if one of the given date as String could not be converted to a Date.
-
isIn
Same assertion asAssert.isIn(Object...)but given dates are represented as anjava.time.Instant.Example:
// assertion will fail // theTwoTowers release date : 2002-12-18 Instant now = Instant.now() assertThat(theTwoTowers.getReleaseDate()).isIn(now, now.plusSeconds(5), now.minusSeconds(5));- Parameters:
instants- the given dates represented asInstant.- Returns:
- this assertion object.
- Throws:
AssertionError- if actual is not in given dates represented asInstant.
-
isInWithStringDateCollection
Same assertion asAssert.isIn(Iterable)but given dates are represented as String either with one of the supported defaults date format or a user custom date format (set with methodwithDateFormat(DateFormat)).User custom date format take precedence over the default ones.
Unless specified otherwise, beware that the default formats are expressed in the current local timezone.
Example:
// assertion will pass // theTwoTowers release date : 2002-12-18 assertThat(theTwoTowers.getReleaseDate()).isInWithStringDateCollection(asList("2002-12-17", "2002-12-18", "2002-12-19")); // assertion will fail assertThat(theTwoTowers.getReleaseDate()).isInWithStringDateCollection(asList("2002-12-17", "2002-12-19", "2002-12-20"))Defaults date format (expressed in the local time zone unless specified otherwise) are:
yyyy-MM-dd'T'HH:mm:ss.SSSX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ss.SSSyyyy-MM-dd HH:mm:ss.SSSyyyy-MM-dd'T'HH:mm:ssX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ssyyyy-MM-dd
Example of valid string date representations:
2003-04-26T03:01:02.758+00:002003-04-26T03:01:02.9992003-04-26 03:01:02.9992003-04-26T03:01:02+00:002003-04-26T13:01:022003-04-26
If you are getting an
IllegalArgumentExceptionwith "Unknown pattern character 'X'" message (some Android versions don't support it), you can explicitly specify the date format to use so that the default ones are bypassed.Method signature could not be
isIn(Collection<String>)because it would be same signature asisIn(Collection<Date>)since java collection type are erased at runtime.- Parameters:
datesAsString- the given Dates represented as String in default or custom date format.- Returns:
- this assertion object.
- Throws:
AssertionError- if actual is not in given Dates represented as String.AssertionError- if one of the given date as String could not be converted to a Date.
-
isNotIn
Same assertion asAssert.isNotIn(Object...)but given dates are represented as String either with one of the supported defaults date format or a user custom date format (set with methodwithDateFormat(DateFormat)).User custom date format take precedence over the default ones.
Unless specified otherwise, beware that the default formats are expressed in the current local timezone.
Example:
// assertion will pass // theTwoTowers release date : 2002-12-18 assertThat(theTwoTowers.getReleaseDate()).isNotIn("2002-12-17", "2002-12-19"); // assertion will fail assertThat(theTwoTowers.getReleaseDate()).isNotIn("2002-12-17", "2002-12-18")Defaults date format (expressed in the local time zone unless specified otherwise) are:
yyyy-MM-dd'T'HH:mm:ss.SSSX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ss.SSSyyyy-MM-dd HH:mm:ss.SSSyyyy-MM-dd'T'HH:mm:ssX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ssyyyy-MM-dd
Example of valid string date representations:
2003-04-26T03:01:02.758+00:002003-04-26T03:01:02.9992003-04-26 03:01:02.9992003-04-26T03:01:02+00:002003-04-26T13:01:022003-04-26
If you are getting an
IllegalArgumentExceptionwith "Unknown pattern character 'X'" message (some Android versions don't support it), you can explicitly specify the date format to use so that the default ones are bypassed.- Parameters:
datesAsString- the given Dates represented as String in default or custom date format.- Returns:
- this assertion object.
- Throws:
AssertionError- if actual is in given Dates represented as String.AssertionError- if one of the given date as String could not be converted to a Date.
-
isNotIn
Same assertion asAssert.isNotIn(Object...)but given dates are represented asjava.time.Instant.Example:
// assertion will pass // theTwoTowers release date : 2002-12-18 Instant now = Instant.now() assertThat(theTwoTowers.getReleaseDate()).isIn(now, now.plusSeconds(5), now.minusSeconds(5));- Parameters:
instants- the given dates represented asInstant.- Returns:
- this assertion object.
- Throws:
AssertionError- if actual is not in given dates represented asInstant.- Since:
- 3.19.0
-
isNotInWithStringDateCollection
Same assertion asAssert.isNotIn(Iterable)but given date is represented as String either with one of the supported default date formats or a user custom date format (set with methodwithDateFormat(DateFormat)).User custom date format takes precedence over the default ones.
Unless specified otherwise, beware that the default formats are expressed in the current local timezone.
Example:
// assertion will pass // theTwoTowers release date : 2002-12-18 assertThat(theTwoTowers.getReleaseDate()).isNotInWithStringDateCollection(Arrays.asList("2002-12-17", "2002-12-19")); // assertion will fail assertThat(theTwoTowers.getReleaseDate()).isNotInWithStringDateCollection(Arrays.asList("2002-12-17", "2002-12-18"))Defaults date format (expressed in the local time zone unless specified otherwise) are:
yyyy-MM-dd'T'HH:mm:ss.SSSX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ss.SSSyyyy-MM-dd HH:mm:ss.SSSyyyy-MM-dd'T'HH:mm:ssX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ssyyyy-MM-dd
Example of valid string date representations:
2003-04-26T03:01:02.758+00:002003-04-26T03:01:02.9992003-04-26 03:01:02.9992003-04-26T03:01:02+00:002003-04-26T13:01:022003-04-26
If you are getting an
IllegalArgumentExceptionwith "Unknown pattern character 'X'" message (some Android versions don't support it), you can explicitly specify the date format to use so that the default ones are bypassed. Method signature could not beisNotIn(Collection<String>)because it would be same signature asisNotIn(Collection<Date>)since java collection type are erased at runtime.- Parameters:
datesAsString- the given Dates represented as String in default or custom date format.- Returns:
- this assertion object.
- Throws:
AssertionError- if actual is in given Dates represented as String.AssertionError- if one of the given date as String could not be converted to a Date.
-
isBefore
Verifies that the actualDateis strictly before the given one.Example:
// assertion will pass // theTwoTowers release date : 2002-12-18 assertThat(theTwoTowers.getReleaseDate()).isBefore(theReturnOfTheKing.getReleaseDate()); // assertion will fail assertThat(theTwoTowers.getReleaseDate()).isBefore(theFellowshipOfTheRing.getReleaseDate());- Parameters:
other- the given Date.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualDateisnull.NullPointerException- if otherDateisnull.AssertionError- if the actualDateis not strictly before the given one.
-
isBefore
Verifies that the actualDateis strictly before the givenInstant.Example:
// assertion succeeds // theTwoTowers release date : 2002-12-18 assertThat(theTwoTowers.getReleaseDate()).isBefore(Instant.parse("2002-12-19T00:00:00.00Z")); // assertions fail assertThat(theTwoTowers.getReleaseDate()).isBefore(Instant.parse("2002-12-17T00:00:00.00Z")); assertThat(theTwoTowers.getReleaseDate()).isBefore(Instant.parse("2002-12-18T00:00:00.00Z"));- Parameters:
other- the givenInstant.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualDateisnull.NullPointerException- if otherInstantisnull.AssertionError- if the actualDateis not strictly before the givenInstant.- Since:
- 3.19.0
-
isBefore
Same assertion asisBefore(Date)but given date is represented as String either with one of the supported default date formats or a user custom date format (set with methodwithDateFormat(DateFormat)).User custom date format takes precedence over the default ones.
Unless specified otherwise, beware that the default formats are expressed in the current local timezone.
Example:
// assertion will pass // theTwoTowers release date : 2002-12-18 assertThat(theTwoTowers.getReleaseDate()).isBefore("2002-12-19"); // assertion will fail assertThat(theTwoTowers.getReleaseDate()).isBefore("2002-12-17"); assertThat(theTwoTowers.getReleaseDate()).isBefore("2002-12-18")Defaults date format (expressed in the local time zone unless specified otherwise) are:
yyyy-MM-dd'T'HH:mm:ss.SSSX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ss.SSSyyyy-MM-dd HH:mm:ss.SSSyyyy-MM-dd'T'HH:mm:ssX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ssyyyy-MM-dd
Example of valid string date representations:
2003-04-26T03:01:02.758+00:002003-04-26T03:01:02.9992003-04-26 03:01:02.9992003-04-26T03:01:02+00:002003-04-26T13:01:022003-04-26
If you are getting an
IllegalArgumentExceptionwith "Unknown pattern character 'X'" message (some Android versions don't support it), you can explicitly specify the date format to use so that the default ones are bypassed.- Parameters:
dateAsString- the given Date represented as String in default or custom date format.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualDateisnull.NullPointerException- if given date as String isnull.AssertionError- if the actualDateis not strictly before the given Date represented as String.AssertionError- if the given date as String could not be converted to a Date.
-
isBeforeOrEqualsTo
Deprecated.prefer callingisBeforeOrEqualTo(Date)Verifies that the actualDateis before or equals to the given one.Example:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); // assertions will pass assertThat(dateFormat.parse("1990-12-01")).isBeforeOrEqualsTo(dateFormat.parse("2000-12-01")); assertThat(dateFormat.parse("2000-12-01")).isBeforeOrEqualsTo(dateFormat.parse("2000-12-01")); // assertion will fail assertThat(dateFormat.parse("2000-12-01")).isBeforeOrEqualsTo(dateFormat.parse("1990-12-01"));- Parameters:
other- the given Date.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualDateisnull.NullPointerException- if otherDateisnull.AssertionError- if the actualDateis not before or equals to the given one.
-
isBeforeOrEqualTo
Verifies that the actualDateis before or equal to the given one.Example:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); // assertions will pass assertThat(dateFormat.parse("1990-12-01")).isBeforeOrEqualTo(dateFormat.parse("2000-12-01")); assertThat(dateFormat.parse("2000-12-01")).isBeforeOrEqualTo(dateFormat.parse("2000-12-01")); // assertion will fail assertThat(dateFormat.parse("2000-12-01")).isBeforeOrEqualTo(dateFormat.parse("1990-12-01"));- Parameters:
other- the given Date.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualDateisnull.NullPointerException- if otherDateisnull.AssertionError- if the actualDateis not before or equals to the given one.
-
isBeforeOrEqualTo
Verifies that the actualDateis before or equal to the givenInstant.Example:
// assertions succeed // theTwoTowers release date : 2002-12-18 assertThat(theTwoTowers.getReleaseDate()).isBeforeOrEqualTo(Instant.parse("2002-12-19T00:00:00.00Z")); assertThat(theTwoTowers.getReleaseDate()).isBeforeOrEqualTo(Instant.parse("2002-12-18T00:00:00.00Z")); // assertion fails assertThat(theTwoTowers.getReleaseDate()).isBeforeOrEqualTo(Instant.parse("2002-12-17T00:00:00.00Z"));- Parameters:
other- the givenInstant.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualDateisnull.NullPointerException- if otherInstantisnull.AssertionError- if the actualDateis not before or equal to the givenInstant.- Since:
- 3.19.0
-
isBeforeOrEqualsTo
Deprecated.prefer callingisBeforeOrEqualTo(String)Same assertion asisBeforeOrEqualsTo(Date)but given date is represented as String either with one of the supported default date formats or a user custom date format (set with methodwithDateFormat(DateFormat)).User custom date format takes precedence over the default ones.
Unless specified otherwise, beware that the default formats are expressed in the current local timezone.
Example:
// assertion will pass // theTwoTowers release date : 2002-12-18 assertThat(theTwoTowers.getReleaseDate()).isBeforeOrEqualsTo("2002-12-19"); assertThat(theTwoTowers.getReleaseDate()).isBeforeOrEqualsTo("2002-12-18"); // assertion will fail assertThat(theTwoTowers.getReleaseDate()).isBeforeOrEqualsTo("2002-12-17")Defaults date format (expressed in the local time zone unless specified otherwise) are:
yyyy-MM-dd'T'HH:mm:ss.SSSX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ss.SSSyyyy-MM-dd HH:mm:ss.SSSyyyy-MM-dd'T'HH:mm:ssX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ssyyyy-MM-dd
Example of valid string date representations:
2003-04-26T03:01:02.758+00:002003-04-26T03:01:02.9992003-04-26 03:01:02.9992003-04-26T03:01:02+00:002003-04-26T13:01:022003-04-26
If you are getting an
IllegalArgumentExceptionwith "Unknown pattern character 'X'" message (some Android versions don't support it), you can explicitly specify the date format to use so that the default ones are bypassed.- Parameters:
dateAsString- the given Date represented as String in default or custom date format.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualDateisnull.NullPointerException- if given date as String isnull.AssertionError- if the actualDateis not before or equals to the given Date represented as String.AssertionError- if the given date as String could not be converted to a Date.
-
isBeforeOrEqualTo
Same assertion asisBeforeOrEqualTo(Date)but given date is represented as String either with one of the supported defaults date format or a user custom date format (set with methodwithDateFormat(DateFormat)).User custom date format take precedence over the default ones.
Unless specified otherwise, beware that the default formats are expressed in the current local timezone.
Example:
// assertion will pass // theTwoTowers release date : 2002-12-18 assertThat(theTwoTowers.getReleaseDate()).isBeforeOrEqualTo("2002-12-19"); assertThat(theTwoTowers.getReleaseDate()).isBeforeOrEqualTo("2002-12-18"); // assertion will fail assertThat(theTwoTowers.getReleaseDate()).isBeforeOrEqualTo("2002-12-17")Defaults date format (expressed in the local time zone unless specified otherwise) are:
yyyy-MM-dd'T'HH:mm:ss.SSSX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ss.SSSyyyy-MM-dd HH:mm:ss.SSSyyyy-MM-dd'T'HH:mm:ssX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ssyyyy-MM-dd
Example of valid string date representations:
2003-04-26T03:01:02.758+00:002003-04-26T03:01:02.9992003-04-26 03:01:02.9992003-04-26T03:01:02+00:002003-04-26T13:01:022003-04-26
If you are getting an
IllegalArgumentExceptionwith "Unknown pattern character 'X'" message (some Android versions don't support it), you can explicitly specify the date format to use so that the default ones are bypassed.- Parameters:
dateAsString- the given Date represented as String in default or custom date format.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualDateisnull.NullPointerException- if given date as String isnull.AssertionError- if the actualDateis not before or equals to the given Date represented as String.AssertionError- if the given date as String could not be converted to a Date.
-
isAfter
Verifies that the actualDateis strictly after the given one.Example:
// assertion will pass // theTwoTowers release date : 2002-12-18 assertThat(theTwoTowers.getReleaseDate()).isAfter(theFellowshipOfTheRing.getReleaseDate()); // assertion will fail assertThat(theTwoTowers.getReleaseDate()).isAfter(theReturnOfTheKing.getReleaseDate());- Parameters:
other- the given Date.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualDateisnull.NullPointerException- if otherDateisnull.AssertionError- if the actualDateis not strictly after the given one.
-
isAfter
Verifies that the actualDateis strictly after the givenInstant.Example:
// assertion succeeds // theTwoTowers release date : 2002-12-18 assertThat(theTwoTowers.getReleaseDate()).isAfter(Instant.parse("2002-12-17T00:00:00.00Z")); // assertions fail assertThat(theTwoTowers.getReleaseDate()).isAfter(Instant.parse("2002-12-18T00:00:00.00Z")); assertThat(theTwoTowers.getReleaseDate()).isAfter(Instant.parse("2002-12-19T00:00:00.00Z"));- Parameters:
other- the givenInstant.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualDateisnull.NullPointerException- if otherInstantisnull.AssertionError- if the actualDateis not strictly after the givenInstant.- Since:
- 3.19.0
-
isAfter
Same assertion asisAfter(Date)but given date is represented as String either with one of the supported default date formats or a user custom date format (set with methodwithDateFormat(DateFormat)).User custom date format takes precedence over the default ones.
Unless specified otherwise, beware that the default formats are expressed in the current local timezone.
Example:
// assertion will pass // theTwoTowers release date : 2002-12-18 assertThat(theTwoTowers.getReleaseDate()).isAfter("2002-12-17"); // assertion will fail assertThat(theTwoTowers.getReleaseDate()).isAfter("2002-12-18"); assertThat(theTwoTowers.getReleaseDate()).isAfter("2002-12-19")Defaults date format (expressed in the local time zone unless specified otherwise) are:
yyyy-MM-dd'T'HH:mm:ss.SSSX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ss.SSSyyyy-MM-dd HH:mm:ss.SSSyyyy-MM-dd'T'HH:mm:ssX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ssyyyy-MM-dd
Example of valid string date representations:
2003-04-26T03:01:02.758+00:002003-04-26T03:01:02.9992003-04-26 03:01:02.9992003-04-26T03:01:02+00:002003-04-26T13:01:022003-04-26
If you are getting an
IllegalArgumentExceptionwith "Unknown pattern character 'X'" message (some Android versions don't support it), you can explicitly specify the date format to use so that the default ones are bypassed.- Parameters:
dateAsString- the given Date represented as String in default or custom date format.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualDateisnull.NullPointerException- if given date as String isnull.AssertionError- if the actualDateis not strictly after the given Date represented as String.AssertionError- if the given date as String could not be converted to a Date.
-
isAfterOrEqualsTo
Deprecated.prefer callingisAfterOrEqualTo(Date)Verifies that the actualDateis after or equals to the given one.Example:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); // assertions will pass assertThat(dateFormat.parse("2000-12-01")).isAfterOrEqualsTo(dateFormat.parse("1990-12-01")); assertThat(dateFormat.parse("2000-12-01")).isAfterOrEqualsTo(dateFormat.parse("2000-12-01")); // assertion will fail assertThat(dateFormat.parse("1990-12-01")).isAfterOrEqualsTo(dateFormat.parse("2000-12-01"));- Parameters:
other- the given Date.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualDateisnull.NullPointerException- if otherDateisnull.AssertionError- if the actualDateis not after or equals to the given one.
-
isAfterOrEqualTo
Verifies that the actualDateis after or equal to the given one.Example:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); // assertions will pass assertThat(dateFormat.parse("2000-12-01")).isAfterOrEqualTo(dateFormat.parse("1990-12-01")); assertThat(dateFormat.parse("2000-12-01")).isAfterOrEqualTo(dateFormat.parse("2000-12-01")); // assertion will fail assertThat(dateFormat.parse("1990-12-01")).isAfterOrEqualTo(dateFormat.parse("2000-12-01"));- Parameters:
other- the given Date.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualDateisnull.NullPointerException- if otherDateisnull.AssertionError- if the actualDateis not after or equals to the given one.
-
isAfterOrEqualTo
Verifies that the actualDateis after or equal to the givenInstant.Example:
// assertions succeed // theTwoTowers release date : 2002-12-18 assertThat(theTwoTowers.getReleaseDate()).assertIsAfterOrEqualTo(Instant.parse("2002-12-17T00:00:00.00Z")) .assertIsAfterOrEqualTo(Instant.parse("2002-12-18T00:00:00.00Z")); // assertion fails assertThat(theTwoTowers.getReleaseDate()).assertIsAfterOrEqualTo(Instant.parse("2002-12-19T00:00:00.00Z"));- Parameters:
other- the givenInstant.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualDateisnull.NullPointerException- if otherInstantisnull.AssertionError- if the actualDateis not after or equal to the givenInstant.- Since:
- 3.19.0
-
isAfterOrEqualsTo
Deprecated.prefer callingisAfterOrEqualTo(String)Same assertion asisAfterOrEqualsTo(Date)but given date is represented as String either with one of the supported default date formats or a user custom date format (set with methodwithDateFormat(DateFormat)).User custom date format takes precedence over the default ones.
Unless specified otherwise, beware that the default formats are expressed in the current local timezone.
Example:
// assertion will pass // theTwoTowers release date : 2002-12-18 assertThat(theTwoTowers.getReleaseDate()).isAfterOrEqualsTo("2002-12-17"); assertThat(theTwoTowers.getReleaseDate()).isAfterOrEqualsTo("2002-12-18"); // assertion will fail assertThat(theTwoTowers.getReleaseDate()).isAfterOrEqualsTo("2002-12-19")Defaults date format (expressed in the local time zone unless specified otherwise) are:
yyyy-MM-dd'T'HH:mm:ss.SSSX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ss.SSSyyyy-MM-dd HH:mm:ss.SSSyyyy-MM-dd'T'HH:mm:ssX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ssyyyy-MM-dd
Example of valid string date representations:
2003-04-26T03:01:02.758+00:002003-04-26T03:01:02.9992003-04-26 03:01:02.9992003-04-26T03:01:02+00:002003-04-26T13:01:022003-04-26
If you are getting an
IllegalArgumentExceptionwith "Unknown pattern character 'X'" message (some Android versions don't support it), you can explicitly specify the date format to use so that the default ones are bypassed.- Parameters:
dateAsString- the given Date represented as String in default or custom date format.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualDateisnull.NullPointerException- if given date as String isnull.AssertionError- if the actualDateis not after or equals to the given Date represented as String.AssertionError- if the given date as String could not be converted to a Date.
-
isAfterOrEqualTo
Same assertion asisAfterOrEqualTo(Date)but given date is represented as String either with one of the supported defaults date format or a user custom date format (set with methodwithDateFormat(DateFormat)).User custom date format take precedence over the default ones.
Unless specified otherwise, beware that the default formats are expressed in the current local timezone.
Example:
// assertion will pass // theTwoTowers release date : 2002-12-18 assertThat(theTwoTowers.getReleaseDate()).isAfterOrEqualTo("2002-12-17"); assertThat(theTwoTowers.getReleaseDate()).isAfterOrEqualTo("2002-12-18"); // assertion will fail assertThat(theTwoTowers.getReleaseDate()).isAfterOrEqualTo("2002-12-19")Defaults date format (expressed in the local time zone unless specified otherwise) are:
yyyy-MM-dd'T'HH:mm:ss.SSSX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ss.SSSyyyy-MM-dd HH:mm:ss.SSSyyyy-MM-dd'T'HH:mm:ssX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ssyyyy-MM-dd
Example of valid string date representations:
2003-04-26T03:01:02.758+00:002003-04-26T03:01:02.9992003-04-26 03:01:02.9992003-04-26T03:01:02+00:002003-04-26T13:01:022003-04-26
If you are getting an
IllegalArgumentExceptionwith "Unknown pattern character 'X'" message (some Android versions don't support it), you can explicitly specify the date format to use so that the default ones are bypassed.- Parameters:
dateAsString- the given Date represented as String in default or custom date format.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualDateisnull.NullPointerException- if given date as String isnull.AssertionError- if the actualDateis not after or equals to the given Date represented as String.AssertionError- if the given date as String could not be converted to a Date.
-
isBetween
Verifies that the actualDateis in [start, end[ period (start included, end excluded).Example:
// assertion will pass // theTwoTowers release date : 2002-12-18 assertThat(theTwoTowers.getReleaseDate()).isBetween(theFellowshipOfTheRing.getReleaseDate(), theReturnOfTheKing.getReleaseDate()); // assertion will fail assertThat(theFellowshipOfTheRing.getReleaseDate()).isBetween(theTwoTowers.getReleaseDate(), theReturnOfTheKing.getReleaseDate());- Parameters:
start- the period start (inclusive), expected not to be null.end- the period end (exclusive), expected not to be null.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualDateisnull.NullPointerException- if startDateisnull.NullPointerException- if endDateisnull.AssertionError- if the actualDateis not in [start, end[ period.
-
isBetween
Same assertion asisBetween(Date, Date)but given date is represented as String either with one of the supported defaults date format or a user custom date format (set with methodwithDateFormat(DateFormat)).User custom date format take precedence over the default ones.
Unless specified otherwise, beware that the default formats are expressed in the current local timezone.
Example:
// assertion will pass // theTwoTowers release date : 2002-12-18 assertThat(theTwoTowers.getReleaseDate()).isBetween("2002-12-17", "2002-12-19"); // assertion will fail assertThat(theTwoTowers.getReleaseDate()).isBetween("2002-12-15", "2002-12-17")Defaults date format (expressed in the local time zone unless specified otherwise) are:
yyyy-MM-dd'T'HH:mm:ss.SSSX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ss.SSSyyyy-MM-dd HH:mm:ss.SSSyyyy-MM-dd'T'HH:mm:ssX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ssyyyy-MM-dd
Example of valid string date representations:
2003-04-26T03:01:02.758+00:002003-04-26T03:01:02.9992003-04-26 03:01:02.9992003-04-26T03:01:02+00:002003-04-26T13:01:022003-04-26
If you are getting an
IllegalArgumentExceptionwith "Unknown pattern character 'X'" message (some Android versions don't support it), you can explicitly specify the date format to use so that the default ones are bypassed.- Parameters:
start- the period start (inclusive), expected not to be null.end- the period end (exclusive), expected not to be null.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualDateisnull.NullPointerException- if start Date as String isnull.NullPointerException- if end Date as String isnull.AssertionError- if the actualDateis not in [start, end[ period.AssertionError- if one of the given date as String could not be converted to a Date.
-
isBetween
Same assertion asisBetween(Date, Date)but given period is represented withInstant.Example:
assertThat(new Date()).isBetween(Instant.now().minusSeconds(5), Instant.now().plusSeconds(5));- Parameters:
start- the period start (inclusive), expected not to be null.end- the period end (exclusive), expected not to be null.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualDateisnull.NullPointerException- if start Instant as String isnull.NullPointerException- if end Instant as String isnull.AssertionError- if the actualDateis not in [start, end[ period.- Since:
- 3.19.0
-
isBetween
Verifies that the actualDateis in the given period defined by start and end dates.
To include start in the period set inclusiveStart parameter totrue.
To include end in the period set inclusiveEnd parameter totrue.
Example:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); // assertions will pass assertThat(format.parse("2000-01-01")).isBetween(format.parse("2000-01-01"), format.parse("2100-12-01"), true, true); assertThat(format.parse("2000-01-01")).isBetween(format.parse("1900-01-01"), format.parse("2000-01-01"), true, true); assertThat(format.parse("2000-01-01")).isBetween(format.parse("1900-01-01"), format.parse("2100-01-01"), false, false); // assertions will fail assertThat(format.parse("2000-01-01")).isBetween(format.parse("2000-01-01"), format.parse("2100-12-01"), false, true); assertThat(format.parse("2000-01-01")).isBetween(format.parse("1900-01-01"), format.parse("2000-01-01"), true, false);- Parameters:
start- the period start, expected not to be null.end- the period end, expected not to be null.inclusiveStart- whether to include start date in period.inclusiveEnd- whether to include end date in period.- Returns:
- this assertion object.
- Throws:
AssertionError- ifactualisnull.NullPointerException- if startDateisnull.NullPointerException- if endDateisnull.AssertionError- if the actualDateis not in (start, end) period.
-
isBetween
Same assertion asisBetween(Date, Date, boolean, boolean)but given date is represented as String either with one of the supported default date formats or a user custom date format (set with methodwithDateFormat(DateFormat)).User custom date format takes precedence over the default ones.
Unless specified otherwise, beware that the default formats are expressed in the current local timezone.
Example:
// assertion will pass // theTwoTowers release date : 2002-12-18 assertThat(theTwoTowers.getReleaseDate()).isBetween("2002-12-17", "2002-12-18", false, true); assertThat(theTwoTowers.getReleaseDate()).isBetween("2002-12-18", "2002-12-19", true, false); // assertion will fail assertThat(theTwoTowers.getReleaseDate()).isBetween("2002-12-17", "2002-12-18", false, false)Defaults date format (expressed in the local time zone unless specified otherwise) are:
yyyy-MM-dd'T'HH:mm:ss.SSSX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ss.SSSyyyy-MM-dd HH:mm:ss.SSSyyyy-MM-dd'T'HH:mm:ssX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ssyyyy-MM-dd
Example of valid string date representations:
2003-04-26T03:01:02.758+00:002003-04-26T03:01:02.9992003-04-26 03:01:02.9992003-04-26T03:01:02+00:002003-04-26T13:01:022003-04-26
If you are getting an
IllegalArgumentExceptionwith "Unknown pattern character 'X'" message (some Android versions don't support it), you can explicitly specify the date format to use so that the default ones are bypassed.- Parameters:
start- the period start, expected not to be null.end- the period end, expected not to be null.inclusiveStart- whether to include start date in period.inclusiveEnd- whether to include end date in period.- Returns:
- this assertion object.
- Throws:
AssertionError- ifactualisnull.NullPointerException- if start Date as String isnull.NullPointerException- if end Date as String isnull.AssertionError- if the actualDateis not in (start, end) period.AssertionError- if one of the given date as String could not be converted to a Date.
-
isBetween
Same assertion asisBetween(Date, Date, boolean, boolean)but given period is represented withInstant.Example:
assertThat(new Date()).isBetween(Instant.now().minusSeconds(5), Instant.now().plusSeconds(5), true, true);- Parameters:
start- the period start, expected not to be null.end- the period end, expected not to be null.inclusiveStart- whether to include start date in period.inclusiveEnd- whether to include end date in period.- Returns:
- this assertion object.
- Throws:
AssertionError- ifactualisnull.NullPointerException- if start Date as Instant isnull.NullPointerException- if end Date as Instant isnull.AssertionError- if the actualDateis not in (start, end) period.- Since:
- 3.19.0
-
isNotBetween
Verifies that the actualDateis not in the given period defined by start and end dates.
To include start in the period set inclusiveStart parameter totrue.
To include end in the period set inclusiveEnd parameter totrue.
Example:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); // assertions will pass assertThat(format.parse("2000-01-01")).isNotBetween(format.parse("2000-01-01"), format.parse("2100-12-01"), false, true); assertThat(format.parse("2000-01-01")).isNotBetween(format.parse("1900-01-01"), format.parse("2000-01-01"), true, false); // assertions will fail assertThat(format.parse("2000-01-01")).isNotBetween(format.parse("2000-01-01"), format.parse("2100-12-01"), true, true); assertThat(format.parse("2000-01-01")).isNotBetween(format.parse("1900-01-01"), format.parse("2000-01-01"), true, true); assertThat(format.parse("2000-01-01")).isNotBetween(format.parse("1900-01-01"), format.parse("2100-01-01"), false, false);- Parameters:
start- the period start (inclusive), expected not to be null.end- the period end (exclusive), expected not to be null.inclusiveStart- whether to include start date in period.inclusiveEnd- whether to include end date in period.- Returns:
- this assertion object.
- Throws:
AssertionError- ifactualisnull.NullPointerException- if startDateisnull.NullPointerException- if endDateisnull.AssertionError- if the actualDateis not in (start, end) period.
-
isNotBetween
Verifies that the actualDateis not in the given period defined by start and end dates.
To include start in the period set inclusiveStart parameter totrue.
To include end in the period set inclusiveEnd parameter totrue.
Example:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); // assertions will pass assertThat(format.parse("2000-01-01")).isNotBetween(format.parse("2000-01-01T00:00:00Z"), format.parse("2100-12-01T00:00:00Z"), false, true); assertThat(format.parse("2000-01-01")).isNotBetween(format.parse("1900-01-01T00:00:00Z"), format.parse("2000-01-01T00:00:00Z"), true, false); // assertions will fail assertThat(format.parse("2000-01-01")).isNotBetween(format.parse("2000-01-01T00:00:00Z"), format.parse("2100-12-01T00:00:00Z"), true, true); assertThat(format.parse("2000-01-01")).isNotBetween(format.parse("1900-01-01T00:00:00Z"), format.parse("2000-01-01T00:00:00Z"), true, true); assertThat(format.parse("2000-01-01")).isNotBetween(format.parse("1900-01-01T00:00:00Z"), format.parse("2100-01-01T00:00:00Z"), false, false);- Parameters:
start- the period start (inclusive), expected not to be null.end- the period end (exclusive), expected not to be null.inclusiveStart- whether to include start date in period.inclusiveEnd- whether to include end date in period.- Returns:
- this assertion object.
- Throws:
AssertionError- ifactualisnull.NullPointerException- if startInstantisnull.NullPointerException- if endInstantisnull.AssertionError- if the actualDateis not in (start, end) period.- Since:
- 3.19.0
-
isNotBetween
Same assertion asisNotBetween(Date, Date, boolean, boolean)but given date is represented as String either with one of the supported default date formats or a user custom date format (set with methodwithDateFormat(DateFormat)).User custom date format takes precedence over the default ones.
Unless specified otherwise, beware that the default formats are expressed in the current local timezone.
Example:
// assertion will pass // theTwoTowers release date : 2002-12-18 assertThat(theTwoTowers.getReleaseDate()).isNotBetween("2002-12-17", "2002-12-18", false, false); // assertion will fail assertThat(theTwoTowers.getReleaseDate()).isNotBetween("2002-12-17", "2002-12-18", false, true); assertThat(theTwoTowers.getReleaseDate()).isNotBetween("2002-12-18", "2002-12-19", true, false)Defaults date format (expressed in the local time zone unless specified otherwise) are:
yyyy-MM-dd'T'HH:mm:ss.SSSX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ss.SSSyyyy-MM-dd HH:mm:ss.SSSyyyy-MM-dd'T'HH:mm:ssX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ssyyyy-MM-dd
Example of valid string date representations:
2003-04-26T03:01:02.758+00:002003-04-26T03:01:02.9992003-04-26 03:01:02.9992003-04-26T03:01:02+00:002003-04-26T13:01:022003-04-26
If you are getting an
IllegalArgumentExceptionwith "Unknown pattern character 'X'" message (some Android versions don't support it), you can explicitly specify the date format to use so that the default ones are bypassed.- Parameters:
start- the period start (inclusive), expected not to be null.end- the period end (exclusive), expected not to be null.inclusiveStart- whether to include start date in period.inclusiveEnd- whether to include end date in period.- Returns:
- this assertion object.
- Throws:
AssertionError- ifactualisnull.NullPointerException- if start Date as String isnull.NullPointerException- if end Date as String isnull.AssertionError- if the actualDateis not in (start, end) period.AssertionError- if one of the given date as String could not be converted to a Date.
-
isNotBetween
Verifies that the actualDateis not in [start, end[ periodExample:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); // assertions will pass assertThat(format.parse("1900-01-01")).isNotBetween(format.parse("2000-01-01"), format.parse("2100-12-01")); assertThat(format.parse("2200-01-01")).isNotBetween(format.parse("2000-01-01"), format.parse("2100-12-01")); assertThat(format.parse("2000-01-01")).isNotBetween(format.parse("1900-01-01"), format.parse("2000-01-01")); // assertions will fail assertThat(format.parse("2001-12-24")).isNotBetween(format.parse("2000-01-01"), format.parse("2100-01-01")); assertThat(format.parse("1900-01-01")).isNotBetween(format.parse("1900-01-01"), format.parse("2000-01-01"));- Parameters:
start- the period start (inclusive), expected not to be null.end- the period end (exclusive), expected not to be null.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualDateisnull.NullPointerException- if startDateisnull.NullPointerException- if endDateisnull.AssertionError- if the actualDateis in [start, end[ period.
-
isNotBetween
Verifies that the actualDateis not in [start, end[ periodExample:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); // assertions will pass assertThat(format.parse("1900-01-01")).isNotBetween(Instant.parse("2000-01-01T00:00:00Z"), Instant.parse("2100-12-01T00:00:00Z")); assertThat(format.parse("2200-01-01")).isNotBetween(Instant.parse("2000-01-01T00:00:00Z"), Instant.parse("2100-12-01T00:00:00Z")); assertThat(format.parse("2000-01-01")).isNotBetween(Instant.parse("1900-01-01T00:00:00Z"), Instant.parse("2000-01-01T00:00:00Z")); // assertions will fail assertThat(format.parse("2001-12-24")).isNotBetween(Instant.parse("2000-01-01T00:00:00Z"), Instant.parse("2100-01-01T00:00:00Z")); assertThat(format.parse("1900-01-01")).isNotBetween(Instant.parse("1900-01-01T00:00:00Z"), Instant.parse("2000-01-01T00:00:00Z"));- Parameters:
start- the period start (inclusive), expected not to be null.end- the period end (exclusive), expected not to be null.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualDateisnull.NullPointerException- if startInstantisnull.NullPointerException- if endInstantisnull.AssertionError- if the actualDateis in [start, end[ period.- Since:
- 3.19.0
-
isNotBetween
Same assertion asisNotBetween(Date, Date)but given date is represented as String either with one of the supported default date formats or a user custom date format (set with methodwithDateFormat(DateFormat)).User custom date format takes precedence over the default ones.
Unless specified otherwise, beware that the default formats are expressed in the current local timezone.
Example:
// assertion will pass assertThat(theFellowshipOfTheRing.getReleaseDate()).isNotBetween("2002-12-01", "2002-12-10"); // assertion will fail assertThat(theFellowshipOfTheRing.getReleaseDate()).isNotBetween("2002-12-01", "2002-12-19")Defaults date format (expressed in the local time zone unless specified otherwise) are:
yyyy-MM-dd'T'HH:mm:ss.SSSX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ss.SSSyyyy-MM-dd HH:mm:ss.SSSyyyy-MM-dd'T'HH:mm:ssX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ssyyyy-MM-dd
Example of valid string date representations:
2003-04-26T03:01:02.758+00:002003-04-26T03:01:02.9992003-04-26 03:01:02.9992003-04-26T03:01:02+00:002003-04-26T13:01:022003-04-26
If you are getting an
IllegalArgumentExceptionwith "Unknown pattern character 'X'" message (some Android versions don't support it), you can explicitly specify the date format to use so that the default ones are bypassed.- Parameters:
start- the period start (inclusive), expected not to be null.end- the period end (exclusive), expected not to be null.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualDateisnull.NullPointerException- if start Date as String isnull.NullPointerException- if end Date as String isnull.AssertionError- if the actualDateis in [start, end[ period.AssertionError- if one of the given date as String could not be converted to a Date.
-
isInThePast
Verifies that the actualDateis strictly in the past.Example:
// assertion will pass assertThat(theTwoTowers.getReleaseDate()).isInThePast();- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualDateisnull.AssertionError- if the actualDateis not in the past.
-
isToday
Verifies that the actualDateis today, that is matching current year, month and day (no check on hour, minute, second, milliseconds).Example:
// assertion will pass assertThat(new Date()).isToday(); // assertion will fail assertThat(theFellowshipOfTheRing.getReleaseDate()).isToday();- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualDateisnull.AssertionError- if the actualDateis not today.
-
isInTheFuture
Verifies that the actualDateis strictly in the future.Example:
Date now = new Date(); // assertion succeeds: assertThat(new Date(now.getTime() + 1000)).isInTheFuture();- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualDateisnull.AssertionError- if the actualDateis not in the future.
-
isBeforeYear
Verifies that the actualDateis strictly before the given year.Example:
// assertion will pass // theTwoTowers release date : 2002-12-18 assertThat(theTwoTowers.getReleaseDate()).isBeforeYear(2004); // assertion will fail assertThat(theTwoTowers.getReleaseDate()).isBeforeYear(2002); assertThat(theTwoTowers.getReleaseDate()).isBeforeYear(2000);- Parameters:
year- the year to compare actual year to- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualDateisnull.AssertionError- if the actualDateyear is after or equals to the given year.
-
isAfterYear
Verifies that the actualDateis strictly after the given year.Example:
// assertion will pass // theTwoTowers release date : 2002-12-18 assertThat(theTwoTowers.getReleaseDate()).isAfterYear(2001); // assertion will fail assertThat(theTwoTowers.getReleaseDate()).isAfterYear(2002); assertThat(theTwoTowers.getReleaseDate()).isAfterYear(2004);- Parameters:
year- the year to compare actual year to- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualDateisnull.AssertionError- if the actualDateyear is before or equals to the given year.
-
hasYear
Verifies that the actualDateyear is equal to the given year.Note that using a
custom comparatorhas no effect on this assertion.Example:
// assertion will pass // theTwoTowers release date : 2002-12-18 assertThat(theTwoTowers.getReleaseDate()).hasYear(2002); // assertion will fail assertThat(theTwoTowers.getReleaseDate()).hasYear(2004);- Parameters:
year- the year to compare actual year to- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualDateisnull.AssertionError- if the actualDateyear is not equal to the given year.
-
isWithinYear
Deprecated.usehasYear(int)instead.- Parameters:
year- the year to compare actual year to- Returns:
- this assertion object.
-
hasMonth
Verifies that the actualDatemonth is equal to the given month, month value starting at 1 (January=1, February=2, ...).Note that using a
custom comparatorhas no effect on this assertion.Example:
// assertion will pass // theTwoTowers release date : 2002-12-18 assertThat(theTwoTowers.getReleaseDate()).hasMonth(12); // assertion will fail assertThat(theTwoTowers.getReleaseDate()).hasMonth(10);- Parameters:
month- the month to compare actual month to, month value starting at 1 (January=1, February=2, ...).- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualDateisnull.AssertionError- if the actualDatemonth is not equal to the given month.
-
isWithinMonth
Deprecated.usehasMonth(int)instead.- Parameters:
month- the month to compare actual month to, month value starting at 1 (January=1, February=2, ...).- Returns:
- this assertion object.
-
hasDayOfMonth
Verifies that the actualDateday of month is equal to the given day of month.Note that using a
custom comparatorhas no effect on this assertion.Example:
// assertion will pass // theTwoTowers release date : 2002-12-18 assertThat(theTwoTowers.getReleaseDate()).hasDayOfMonth(18); // assertion will fail assertThat(theTwoTowers.getReleaseDate()).hasDayOfMonth(20);- Parameters:
dayOfMonth- the day of month to compare actual day of month to- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualDateisnull.AssertionError- if the actualDatemonth is not equal to the given day of month.
-
isWithinDayOfMonth
Deprecated.usehasDayOfMonth(int)instead.- Parameters:
dayOfMonth- the day of month to compare actual day of month to- Returns:
- this assertion object.
-
hasDayOfWeek
Verifies that the actualDateday of week is equal to the given day of week (seeCalendar.DAY_OF_WEEKfor valid values).Note that using a
custom comparatorhas no effect on this assertion.Example:
// assertion will pass assertThat(new Date(parseDatetime("2003-04-26T13:20:35").getTime()).hasDayOfWeek(Calendar.SATURDAY); // assertion will fail assertThat(new Date(parseDatetime("2003-04-26T13:20:35").getTime()).hasDayOfWeek(Calendar.MONDAY);- Parameters:
dayOfWeek- the day of week to compare actual day of week to, seeCalendar.DAY_OF_WEEKfor valid values- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualDateisnull.AssertionError- if the actualDateweek is not equal to the given day of week.
-
isWithinDayOfWeek
Deprecated.usehasDayOfWeek(int)instead.- Parameters:
dayOfWeek- the day of week to compare actual day of week to, seeCalendar.DAY_OF_WEEKfor valid values- Returns:
- this assertion object.
-
hasHourOfDay
Verifies that the actualDatehour of day is equal to the given hour of day (24-hour clock).Note that using a
custom comparatorhas no effect on this assertion.Example:
// assertion will pass assertThat(new Date(parseDatetime("2003-04-26T13:20:35").getTime()).hasHourOfDay(13); // assertion will fail assertThat(new Date(parseDatetime("2003-04-26T13:20:35").getTime()).hasHourOfDay(22);- Parameters:
hourOfDay- the hour of day to compare actual hour of day to (24-hour clock)- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualDateisnull.AssertionError- if the actualDatehour is not equal to the given hour.
-
isWithinHourOfDay
Deprecated.usehasHourOfDay(int)instead.- Parameters:
hourOfDay- the hour of day to compare actual hour of day to (24-hour clock)- Returns:
- this assertion object.
-
hasMinute
Verifies that the actualDateminute is equal to the given minute.Note that using a
custom comparatorhas no effect on this assertion.Example:
// assertion will pass assertThat(new Date(parseDatetime("2003-04-26T13:20:35").getTime()).hasMinute(20); // assertion will fail assertThat(new Date(parseDatetime("2003-04-26T13:20:35").getTime()).hasMinute(17);- Parameters:
minute- the minute to compare actual minute to- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualDateisnull.AssertionError- if the actualDateminute is not equal to the given minute.
-
isWithinMinute
Deprecated.usehasMinute(int)instead.- Parameters:
minute- the minute to compare actual minute to- Returns:
- this assertion object.
-
hasSecond
Verifies that the actualDatesecond is equal to the given second.Note that using a
custom comparatorhas no effect on this assertion.Example:
// assertion will pass assertThat(new Date(parseDatetime("2003-04-26T13:20:35").getTime()).hasSecond(35); // assertion will fail assertThat(new Date(parseDatetime("2003-04-26T13:20:35").getTime()).hasSecond(11);- Parameters:
second- the second to compare actual second to- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualDateisnull.AssertionError- if the actualDatesecond is not equal to the given second.
-
isWithinSecond
Deprecated.usehasSecond(int)instead.- Parameters:
second- the second to compare actual second to- Returns:
- this assertion object.
-
hasMillisecond
Verifies that the actualDatemillisecond is equal to the given millisecond.Examples:
Note that using a// assertion will pass assertThat(parseDatetimeWithMs("2003-04-26T13:20:35.017")).hasMillisecond(17); // assertion will fail assertThat(parseDatetimeWithMs("2003-04-26T13:20:35.017")).hasMillisecond(25);custom comparatorhas no effect on this assertion.- Parameters:
millisecond- the millisecond to compare actual millisecond to- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualDateisnull.AssertionError- if the actualDatemillisecond is not equal to the given millisecond.
-
isWithinMillisecond
Deprecated.usehasMillisecond(int)instead.- Parameters:
millisecond- the millisecond to compare actual millisecond to- Returns:
- this assertion object.
-
isInSameYearAs
Verifies that actual and givenDateare in the same year.Example:
Note that using aDate date1 = parse("2003-04-26"); Date date2 = parse("2003-05-27"); assertThat(date1).isInSameYearAs(date2);custom comparatorhas no effect on this assertion.- Parameters:
other- the givenDateto compare actualDateto.- Returns:
- this assertion object.
- Throws:
NullPointerException- ifDateparameter isnull.AssertionError- if the actualDateisnull.AssertionError- if actual and givenDateare not in the same year.
-
isInSameYearAs
Verifies that actualDateand givenInstantare in the same year.Example:
Note that using aDate date = parse("2003-04-26"); Instant instant = Instant.parse("2003-04-26T12:30:00Z"); assertThat(date).isInSameYearAs(instant);custom comparatorhas no effect on this assertion.- Parameters:
other- the givenInstantto compare actualDateto.- Returns:
- this assertion object.
- Throws:
NullPointerException- ifInstantparameter isnull.AssertionError- if the actualDateisnull.AssertionError- if actualDateand givenInstantare not in the same year.- Since:
- 3.19.0
-
isInSameYearAs
Same assertion asisInSameYearAs(Date)but given date is represented as String either with one of the supported defaults date format or a user custom date format (set with methodwithDateFormat(DateFormat)).User custom date format take precedence over the default ones.
Unless specified otherwise, beware that the default formats are expressed in the current local timezone.
Example:
Date date1 = parse("2003-04-26"); assertThat(date1).isInSameYearAs("2003-05-27")Defaults date format (expressed in the local time zone unless specified otherwise) are:
yyyy-MM-dd'T'HH:mm:ss.SSSX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ss.SSSyyyy-MM-dd HH:mm:ss.SSSyyyy-MM-dd'T'HH:mm:ssX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ssyyyy-MM-dd
Example of valid string date representations:
2003-04-26T03:01:02.758+00:002003-04-26T03:01:02.9992003-04-26 03:01:02.9992003-04-26T03:01:02+00:002003-04-26T13:01:022003-04-26
If you are getting an
IllegalArgumentExceptionwith "Unknown pattern character 'X'" message (some Android versions don't support it), you can explicitly specify the date format to use so that the default ones are bypassed.- Parameters:
dateAsString- the given Date represented as String in default or custom date format.- Returns:
- this assertion object.
- Throws:
NullPointerException- if dateAsString parameter isnull.AssertionError- if the actualDateisnull.AssertionError- if actual and given Date represented as String are not in the same year.AssertionError- if the given date as String could not be converted to a Date.
-
isInSameMonthAs
Verifies that actual and givenDatehave same month and year fields.Example:
Note that using aDate date1 = parse("2003-04-26"); Date date2 = parse("2003-04-27"); assertThat(date1).isInSameMonthAs(date2);custom comparatorhas no effect on this assertion.- Parameters:
other- the givenDateto compare actualDateto.- Returns:
- this assertion object.
- Throws:
NullPointerException- ifDateparameter isnull.AssertionError- if the actualDateisnull.AssertionError- if actual and givenDateare not in the same month and year.
-
isInSameMonthAs
Verifies that actualDateand givenInstanthave same month and year fields.Example:
Date date = parse("2003-04-26"); Instant instant = Instant.parse("2003-04-27T12:30:00Z"); assertThat(date).isInSameMonthAs(instant);Note that using a
custom comparatorhas no effect on this assertion.- Parameters:
other- the givenInstantto compare actualDateto.- Returns:
- this assertion object.
- Throws:
NullPointerException- ifInstantparameter isnull.AssertionError- if the actualDateisnull.AssertionError- if actualDateand givenInstantare not in the same month and year.- Since:
- 3.19.0
-
isInSameMonthAs
Same assertion asisInSameMonthAs(Date)but given date is represented as String either with one of the supported default date formats or a user custom date format (set with methodwithDateFormat(DateFormat)).User custom date format takes precedence over the default ones.
Unless specified otherwise, beware that the default formats are expressed in the current local timezone.
Example:
Date date1 = parse("2003-04-26"); assertThat(date1).isInSameMonthAs("2003-04-27")Defaults date format (expressed in the local time zone unless specified otherwise) are:
yyyy-MM-dd'T'HH:mm:ss.SSSX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ss.SSSyyyy-MM-dd HH:mm:ss.SSSyyyy-MM-dd'T'HH:mm:ssX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ssyyyy-MM-dd
Example of valid string date representations:
2003-04-26T03:01:02.758+00:002003-04-26T03:01:02.9992003-04-26 03:01:02.9992003-04-26T03:01:02+00:002003-04-26T13:01:022003-04-26
If you are getting an
IllegalArgumentExceptionwith "Unknown pattern character 'X'" message (some Android versions don't support it), you can explicitly specify the date format to use so that the default ones are bypassed.- Parameters:
dateAsString- the given Date represented as String in default or custom date format.- Returns:
- this assertion object.
- Throws:
NullPointerException- if dateAsString parameter isnull.AssertionError- if the actualDateisnull.AssertionError- if actual and givenDateare not in the same month.
-
isInSameDayAs
Verifies that actual and givenDatehave the same day of month, month and year fields values.Example:
Date date1 = parseDatetime("2003-04-26T23:17:00"); Date date2 = parseDatetime("2003-04-26T12:30:00"); assertThat(date1).isInSameDayAs(date2);Note that using a
custom comparatorhas no effect on this assertion.This assertion is logically equivalent to
isEqualToIgnoringHours(Date).- Parameters:
other- the givenDateto compare actualDateto.- Returns:
- this assertion object.
- Throws:
NullPointerException- ifDateparameter isnull.AssertionError- if the actualDateisnull.AssertionError- if actual and givenDateare not in the same day, month and year.
-
isInSameDayAs
Verifies that actualDateand givenInstanthave the same day of month, month and year fields values.Example:
Note that using aDate date = parseDatetime("2003-04-26T23:17:00"); Instant instant = Instant.parse("2003-04-26T12:30:00Z"); assertThat(date).isInSameDayAs(instant);custom comparatorhas no effect on this assertion.This assertion is logically equivalent to
isEqualToIgnoringHours(Instant).- Parameters:
other- the givenDateto compare actualDateto.- Returns:
- this assertion object.
- Throws:
NullPointerException- ifInstantparameter isnull.AssertionError- if the actualDateisnull.AssertionError- if actualDateand givenInstantare not in the same day, month and year.- Since:
- 3.19.0
-
isInSameDayAs
Same assertion asisInSameDayAs(Date)but given date is represented as String either with one of the supported default date formats or a user custom date format (set with methodwithDateFormat(DateFormat)).User custom date format takes precedence over the default ones.
Unless specified otherwise, beware that the default formats are expressed in the current local timezone.
Example:
Date date1 = parseDatetime("2003-04-26T23:17:00"); assertThat(date1).isInSameDayAs("2003-04-26")Defaults date format (expressed in the local time zone unless specified otherwise) are:
yyyy-MM-dd'T'HH:mm:ss.SSSX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ss.SSSyyyy-MM-dd HH:mm:ss.SSSyyyy-MM-dd'T'HH:mm:ssX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ssyyyy-MM-dd
Example of valid string date representations:
2003-04-26T03:01:02.758+00:002003-04-26T03:01:02.9992003-04-26 03:01:02.9992003-04-26T03:01:02+00:002003-04-26T13:01:022003-04-26
If you are getting an
IllegalArgumentExceptionwith "Unknown pattern character 'X'" message (some Android versions don't support it), you can explicitly specify the date format to use so that the default ones are bypassed.This assertion is logically equivalent to
isEqualToIgnoringHours(String).- Parameters:
dateAsString- the given Date represented as String in default or custom date format.- Returns:
- this assertion object.
- Throws:
NullPointerException- if dateAsString parameter isnull.AssertionError- if the actualDateisnull.AssertionError- if actual and givenDateare not in the same day of month.
-
isInSameHourWindowAs
Verifies that actual and givenDateare chronologically in the same hour (i.e. their time difference < 1 hour).This assertion succeeds as time difference is exactly < 1h:
Two dates can have different hour fields and yet be in the same chronological hour, example:Date date1 = parseDatetime("2003-04-26T13:00:00"); Date date2 = parseDatetime("2003-04-26T13:30:00"); assertThat(date1).isInSameHourWindowAs(date2);
These assertions fail as time difference is equal to or greater than one hour:Date date1 = parseDatetime("2003-04-26T13:00:00"); Date date2 = parseDatetime("2003-04-26T12:59:59"); // succeeds as time difference == 1s assertThat(date1).isInSameHourWindowAs(date2);Date date1 = parseDatetime("2003-04-26T13:00:00"); Date date2 = parseDatetime("2003-04-26T14:00:01"); Date date3 = parseDatetime("2003-04-26T14:00:00"); assertThat(date1).isInSameHourWindowAs(date2); assertThat(date1).isInSameHourWindowAs(date3);Note that using a
custom comparatorhas no effect on this assertion.- Parameters:
other- the givenDateto compare actualDateto.- Returns:
- this assertion object.
- Throws:
NullPointerException- ifDateparameter isnull.AssertionError- if the actualDateisnull.AssertionError- if actual and givenDateare not in the same hour.
-
isInSameHourWindowAs
Verifies that actualDateand givenInstantare chronologically in the same hour (i.e. their time difference < 1 hour).This assertion succeeds as time difference is exactly = 1h:
Two date/instant can have different hour fields and yet be in the same chronological hour, example:Date date = parseDatetime("2003-04-26T13:00:00Z"); Instant instant = Instant.parse("2003-04-26T14:00:00Z"); assertThat(date).isInSameHourWindowAs(instant);
These assertions fail as time difference is equal to or greater than one hour:Date date = parseDatetime("2003-04-26T13:00:00Z"); Instant instant = Instant.parse("2003-04-26T12:59:59Z"); // succeeds as time difference == 1s assertThat(date).isInSameHourWindowAs(instant);Date date = parseDatetime("2003-04-26T13:00:00Z"); Instant instant = Instant.parse("2003-04-26T14:00:01Z"); Instant instant2 = Instant.parse("2003-04-26T14:00:00Z"); assertThat(date).isInSameHourWindowAs(instant); assertThat(date).isInSameHourWindowAs(instant2);Note that using a
custom comparatorhas no effect on this assertion.- Parameters:
other- the givenInstantto compare actualDateto.- Returns:
- this assertion object.
- Throws:
NullPointerException- ifInstantparameter isnull.AssertionError- if the actualDateisnull.AssertionError- if actualDateand givenInstantare not in the same hour.- Since:
- 3.19.0
-
isInSameHourWindowAs
Same assertion asisInSameHourWindowAs(java.util.Date)but given date is represented as String either with one of the supported default date formats or a user custom date format (set with methodwithDateFormat(DateFormat)).User custom date format takes precedence over the default ones.
Unless specified otherwise, beware that the default formats are expressed in the current local timezone.
Defaults date format (expressed in the local time zone unless specified otherwise) are:
yyyy-MM-dd'T'HH:mm:ss.SSSX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ss.SSSyyyy-MM-dd HH:mm:ss.SSSyyyy-MM-dd'T'HH:mm:ssX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ssyyyy-MM-dd
Example of valid string date representations:
2003-04-26T03:01:02.758+00:002003-04-26T03:01:02.9992003-04-26 03:01:02.9992003-04-26T03:01:02+00:002003-04-26T13:01:022003-04-26
If you are getting an
IllegalArgumentExceptionwith "Unknown pattern character 'X'" message (some Android versions don't support it), you can explicitly specify the date format to use so that the default ones are bypassed.- Parameters:
dateAsString- the given Date represented as String in default or custom date format.- Returns:
- this assertion object.
- Throws:
NullPointerException- if dateAsString parameter isnull.AssertionError- if the actualDateisnull.AssertionError- if actual and givenDateare not in the same day of month.
-
isInSameHourAs
Deprecated.UseisCloseTo(Date, long)instead, although not exactly the same semantics, this is the right way to compare with a given precision.Verifies that actual and givenDatehave same hour, day, month and year fields values.Example:
This assertion does not make a true chronological comparison since two dates can have different hour fields and yet be in the same chronological hour, e.g:Date date1 = parseDatetime("2003-01-01T12:00:00"); Date date2 = parseDatetime("2003-01-01T12:30:00"); // succeeds assertThat(date1).isInSameHourAs(date2);
If you want to assert that two dates are chronologically in the same hour time window use// dates in the same hour time window but with different hour fields Date date1 = parseDatetime("2003-01-01T12:00:00"); Date date2 = parseDatetime("2003-01-01T11:59:00");isInSameHourWindowAsassertion (note that ifisInSameHourAssucceeds thenisInSameHourWindowAswill succeed too).Note that using a
custom comparatorhas no effect on this assertion.This assertion is logically equivalent to
isEqualToIgnoringMinutes(Date).- Parameters:
other- the givenDateto compare actualDateto.- Returns:
- this assertion object.
- Throws:
NullPointerException- ifDateparameter isnull.AssertionError- if the actualDateisnull.AssertionError- if actual and givenDateare not in the same hour, day, month and year.
-
isInSameHourAs
Deprecated.UseisCloseTo(Date, long)instead, although not exactly the same semantics, this is the right way to compare with a given precision.Same assertion asisInSameHourAs(Date)but given date is represented as String either with one of the supported default date formats or a user custom date format (set with methodwithDateFormat(DateFormat)).User custom date format takes precedence over the default ones.
Unless specified otherwise, beware that the default formats are expressed in the current local timezone.
Defaults date format (expressed in the local time zone unless specified otherwise) are:
yyyy-MM-dd'T'HH:mm:ss.SSSX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ss.SSSyyyy-MM-dd HH:mm:ss.SSSyyyy-MM-dd'T'HH:mm:ssX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ssyyyy-MM-dd
Example of valid string date representations:
2003-04-26T03:01:02.758+00:002003-04-26T03:01:02.9992003-04-26 03:01:02.9992003-04-26T03:01:02+00:002003-04-26T13:01:022003-04-26
If you are getting an
IllegalArgumentExceptionwith "Unknown pattern character 'X'" message (some Android versions don't support it), you can explicitly specify the date format to use so that the default ones are bypassed.This assertion is logically equivalent to
isEqualToIgnoringMinutes(String).- Parameters:
dateAsString- the given Date represented as String in default or custom date format.- Returns:
- this assertion object.
- Throws:
NullPointerException- if dateAsString parameter isnull.AssertionError- if the actualDateisnull.AssertionError- if actual and givenDateare not in the same hour.
-
isInSameMinuteWindowAs
Verifies that actual and givenDateare chronologically in the same minute (i.e. their time difference < 1 minute).Example:
Two dates can have different minute fields and yet be in the same chronological minute, example:Date date1 = parseDatetime("2003-01-01T12:01:00"); Date date2 = parseDatetime("2003-01-01T12:01:30"); // succeeds because date time difference < 1 min assertThat(date1).isInSameMinuteWindowAs(date2);
This assertion fails as time difference is ≥ 1 min:Date date1 = parseDatetime("2003-01-01T12:01:00"); Date date3 = parseDatetime("2003-01-01T12:00:59"); // succeeds as time difference == 1s even though minute fields differ assertThat(date1).isInSameMinuteWindowAs(date3);
Note that using aDate date1 = parseDatetime("2003-01-01T12:01:00"); Date date2 = parseDatetime("2003-01-01T12:02:00"); assertThat(date1).isInSameMinuteWindowAs(date2);custom comparatorhas no effect on this assertion.- Parameters:
other- the givenDateto compare actualDateto.- Returns:
- this assertion object.
- Throws:
NullPointerException- ifDateparameter isnull.AssertionError- if the actualDateisnull.AssertionError- if actual and givenDateare not in the same minute.
-
isInSameMinuteWindowAs
Verifies that actualDateand givenInstantare chronologically in the same minute (i.e. their time difference < 1 minute).Example:
Two date/instant can have different minute fields and yet be in the same chronological minute, example:Date date = parseDatetime("2003-01-01T12:01:00Z"); Instant instant = Instant.parse("2003-01-01T12:01:30Z"); // succeeds because date time difference < 1 min assertThat(date).isInSameMinuteWindowAs(instant);
This assertion fails as time difference is ≥ 1 min:Date date = parseDatetime("2003-01-01T12:01:00Z"); Instant instant = Instant.parse("2003-01-01T12:00:59Z"); // succeeds as time difference == 1s even though minute fields differ assertThat(date).isInSameMinuteWindowAs(instant);
Note that using aDate date = parseDatetime("2003-01-01T12:01:00Z"); Instant instant = Instant.parse("2003-01-01T12:02:00Z"); assertThat(date).isInSameMinuteWindowAs(instant);custom comparatorhas no effect on this assertion.- Parameters:
other- the givenInstantto compare actualDateto.- Returns:
- this assertion object.
- Throws:
NullPointerException- ifInstantparameter isnull.AssertionError- if the actualDateisnull.AssertionError- if actualDateand givenInstantare not in the same minute.- Since:
- 3.19.0
-
isInSameMinuteWindowAs
Same assertion asisInSameMinuteWindowAs(Date)but given date is represented as String either with one of the supported default date formats or a user custom date format (set with methodwithDateFormat(DateFormat)).User custom date format takes precedence over the default ones.
Unless specified otherwise, beware that the default formats are expressed in the current local timezone.
Defaults date format (expressed in the local time zone unless specified otherwise) are:
yyyy-MM-dd'T'HH:mm:ss.SSSX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ss.SSSyyyy-MM-dd HH:mm:ss.SSSyyyy-MM-dd'T'HH:mm:ssX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ssyyyy-MM-dd
Example of valid string date representations:
2003-04-26T03:01:02.758+00:002003-04-26T03:01:02.9992003-04-26 03:01:02.9992003-04-26T03:01:02+00:002003-04-26T13:01:022003-04-26
If you are getting an
IllegalArgumentExceptionwith "Unknown pattern character 'X'" message (some Android versions don't support it), you can explicitly specify the date format to use so that the default ones are bypassed.- Parameters:
dateAsString- the given Date represented as String in default or custom date format.- Returns:
- this assertion object.
- Throws:
NullPointerException- if dateAsString parameter isnull.AssertionError- if the actualDateisnull.AssertionError- if actual and givenDateare not in the same minute.
-
isInSameMinuteAs
Deprecated.UseisCloseTo(Date, long)instead, although not exactly the same semantics, this is the right way to compare with a given precision.Verifies that actual and givenDatehave same minute, same hour, day, month and year fields values.Example:
It does not make a true chronological comparison since two dates can have different minute fields and yet be in the same chronological minute, e.g:Date date1 = parseDatetime("2003-01-01T12:01:00"); Date date2 = parseDatetime("2003-01-01T12:01:30"); // succeeds because the all the fields up to minutes are the same assertThat(date1).isInSameMinuteAs(date2);
If you want to assert that two dates are in the same minute time window use// dates in the same minute time window but with different minute fields Date date1 = parseDatetime("2003-01-01T12:01:00"); Date date3 = parseDatetime("2003-01-01T12:00:59"); // fails because minutes fields differ even though time difference is only 1s ! assertThat(date1).isInSameMinuteAs(date3); // ERRORisInSameMinuteWindowAsassertion (note that ifisInSameMinuteAssucceeds thenisInSameMinuteWindowAswill succeed too).Note that using a
custom comparatorhas no effect on this assertion.This assertion is logically equivalent to
isEqualToIgnoringSeconds(Date).- Parameters:
other- the givenDateto compare actualDateto.- Returns:
- this assertion object.
- Throws:
NullPointerException- ifDateparameter isnull.AssertionError- if the actualDateisnull.AssertionError- if actual and givenDateare not in the same minute.
-
isInSameMinuteAs
Deprecated.UseisCloseTo(Date, long)instead, although not exactly the same semantics, this is the right way to compare with a given precision.Same assertion asisInSameMinuteAs(Date)but given date is represented as String either with one of the supported default date formats or a user custom date format (set with methodwithDateFormat(DateFormat)).User custom date format takes precedence over the default ones.
Unless specified otherwise, beware that the default formats are expressed in the current local timezone.
Default date formats (expressed in the local time zone unless specified otherwise) are:
yyyy-MM-dd'T'HH:mm:ss.SSSX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ss.SSSyyyy-MM-dd HH:mm:ss.SSSyyyy-MM-dd'T'HH:mm:ssX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ssyyyy-MM-dd
Example of valid string date representations:
2003-04-26T03:01:02.758+00:002003-04-26T03:01:02.9992003-04-26 03:01:02.9992003-04-26T03:01:02+00:002003-04-26T13:01:022003-04-26
If you are getting an
IllegalArgumentExceptionwith "Unknown pattern character 'X'" message (some Android versions don't support it), you can explicitly specify the date format to use so that the default ones are bypassed.This assertion is logically equivalent to
isEqualToIgnoringSeconds(String).- Parameters:
dateAsString- the given Date represented as String in default or custom date format.- Returns:
- this assertion object.
- Throws:
NullPointerException- if dateAsString parameter isnull.AssertionError- if the actualDateisnull.AssertionError- if actual and givenDateare not in the same minute.
-
isInSameSecondWindowAs
Verifies that actual and givenDateare chronologically strictly in the same second (i.e. their time difference < 1 second).Example:
Two dates can have different second fields and yet be in the same chronological second, example:Date date1 = parseDatetimeWithMs("2003-04-26T13:01:02.123"); Date date2 = parseDatetimeWithMs("2003-04-26T13:01:02.456"); // succeeds as time difference is < 1s assertThat(date1).isInSameSecondWindowAs(date2);
Those assertions fail as time difference is greater or equal to one second:Date date1 = parseDatetimeWithMs("2003-04-26T13:01:02.999"); Date date2 = parseDatetimeWithMs("2003-04-26T13:01:03.000"); // succeeds as time difference is 1ms < 1s assertThat(date1).isInSameSecondWindowAs(date2);
Note that using aDate date1 = parseDatetimeWithMs("2003-04-26T13:01:01.000"); Date date2 = parseDatetimeWithMs("2003-04-26T13:01:02.000"); // fails as time difference = 1s assertThat(date1).isInSameSecondWindowAs(date2); Date date3 = parseDatetimeWithMs("2003-04-26T13:01:02.001"); // fails as time difference > 1s assertThat(date1).isInSameSecondWindowAs(date3);custom comparatorhas no effect on this assertion.- Parameters:
other- the givenDateto compare actualDateto.- Returns:
- this assertion object.
- Throws:
NullPointerException- ifDateparameter isnull.AssertionError- if the actualDateisnull.AssertionError- if actual and givenDateare not in the same second.
-
isInSameSecondWindowAs
Verifies that actualDateand givenInstantare chronologically strictly in the same second (i.e. their time difference < 1 second).Example:
Two dates can have different second fields and yet be in the same chronological second, example:Date date = parseDatetimeWithMs("2003-04-26T13:01:02.123Z"); Instant instant = Instant.parse("2003-04-26T13:01:02.456Z"); // succeeds as time difference is < 1s assertThat(date).isInSameSecondWindowAs(instant);
Those assertions fail as time difference is greater or equal to one second:Date date = parseDatetimeWithMs("2003-04-26T13:01:02.999Z"); Instant instant = Instant.parse("2003-04-26T13:01:03.000Z"); // succeeds as time difference is 1ms < 1s assertThat(date).isInSameSecondWindowAs(instant);
Note that using aDate date = parseDatetimeWithMs("2003-04-26T13:01:01.000Z"); Instant instant = Instant.parse("2003-04-26T13:01:02.000Z"); // fails as time difference = 1s assertThat(date).isInSameSecondWindowAs(instant); Instant instant2 = Instant.parse("2003-04-26T13:01:02.001Z"); // fails as time difference > 1s assertThat(date).isInSameSecondWindowAs(instant2);custom comparatorhas no effect on this assertion.- Parameters:
other- the givenInstantto compare actualDateto.- Returns:
- this assertion object.
- Throws:
NullPointerException- ifInstantparameter isnull.AssertionError- if the actualDateisnull.AssertionError- if actualDateand givenInstantare not in the same second.- Since:
- 3.19.0
-
isInSameSecondWindowAs
Same assertion asisInSameSecondWindowAs(Date)but given date is represented as String either with one of the supported default date formats or a user custom date format (set with methodwithDateFormat(DateFormat)).User custom date format takes precedence over the default ones.
Unless specified otherwise, beware that the default formats are expressed in the current local timezone.
Defaults date format (expressed in the local time zone unless specified otherwise) are:
yyyy-MM-dd'T'HH:mm:ss.SSSX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ss.SSSyyyy-MM-dd HH:mm:ss.SSSyyyy-MM-dd'T'HH:mm:ssX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ssyyyy-MM-dd
Example of valid string date representations:
2003-04-26T03:01:02.758+00:002003-04-26T03:01:02.9992003-04-26 03:01:02.9992003-04-26T03:01:02+00:002003-04-26T13:01:022003-04-26
If you are getting an
IllegalArgumentExceptionwith "Unknown pattern character 'X'" message (some Android versions don't support it), you can explicitly specify the date format to use so that the default ones are bypassed.- Parameters:
dateAsString- the given Date represented as String.- Returns:
- this assertion object.
- Throws:
NullPointerException- if dateAsString parameter isnull.AssertionError- if the actualDateisnull.AssertionError- if actual and givenDateare not in the same second.
-
isInSameSecondAs
Deprecated.UseisCloseTo(Date, long)instead, although not exactly the same semantics, this is the right way to compare with a given precision.Verifies that actual and givenDatehave same second, minute, hour, day, month and year fields values.
It does not make a true chronological comparison since two dates can have different second fields and yet be in the same chronological second, e.g:Date date1 = parseDatetimeWithMs("2003-01-01T12:00:01.000"); Date date2 = parseDatetimeWithMs("2003-01-01T12:00:01.250"); // succeeds because the all the time fields up to seconds are the same assertThat(date1).isInSameSecondAs(date2);
If you want to assert that two dates are in the same second time window useDate date1 = parseDatetimeWithMs("2003-01-01T12:00:01.000"); Date date3 = parseDatetimeWithMs("2003-01-01T12:00:00.999"); // fails because seconds fields differ even though time difference is only 1ms ! assertThat(date1).isInSameSecondAs(date3); // ERRORisInSameSecondWindowAsassertion.If you want to compare second fields only (without minute, hour, day, month and year), you could write :
assertThat(myDate).hasSecond(secondOf(otherDate))
usingDateUtil.secondOf(Date)to get the second of a given Date.Note that using a
custom comparatorhas no effect on this assertion.This assertion is logically equivalent to
isEqualToIgnoringMillis(Date).- Parameters:
other- the givenDateto compare actualDateto.- Returns:
- this assertion object.
- Throws:
NullPointerException- ifDateparameter isnull.AssertionError- if the actualDateisnull.AssertionError- if actual and givenDateare not in the same second.
-
isInSameSecondAs
Deprecated.UseisCloseTo(Date, long)instead, although not exactly the same semantics, this is the right way to compare with a given precision.Same assertion asisInSameSecondAs(Date)but given date is represented as String either with one of the supported defaults date format or a user custom date format (set with methodwithDateFormat(DateFormat)).User custom date format take precedence over the default ones.
Unless specified otherwise, beware that the default formats are expressed in the current local timezone.
Defaults date format (expressed in the local time zone unless specified otherwise) are:
yyyy-MM-dd'T'HH:mm:ss.SSSX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ss.SSSyyyy-MM-dd HH:mm:ss.SSSyyyy-MM-dd'T'HH:mm:ssX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ssyyyy-MM-dd
Example of valid string date representations:
2003-04-26T03:01:02.758+00:002003-04-26T03:01:02.9992003-04-26 03:01:02.9992003-04-26T03:01:02+00:002003-04-26T13:01:022003-04-26
If you are getting an
IllegalArgumentExceptionwith "Unknown pattern character 'X'" message (some Android versions don't support it), you can explicitly specify the date format to use so that the default ones are bypassed.This assertion is logically equivalent to
isEqualToIgnoringMillis(String).- Parameters:
dateAsString- the given Date represented as String.- Returns:
- this assertion object.
-
isCloseTo
Verifies that the actualDateis close to the other date by less than delta (expressed in milliseconds), if difference is equal to delta it's ok.One can use handy
TimeUnitto convert a duration in milliseconds, for example you can express a delta of 5 seconds withTimeUnit.SECONDS.toMillis(5).Note that using a
custom comparatorhas no effect on this assertion.Example:
Date date1 = new Date(); Date date2 = new Date(date1.getTime() + 100); // assertion succeeds assertThat(date1).isCloseTo(date2, 101) .isCloseTo(date2, 100); // assertion fails assertThat(date1).isCloseTo(date2, 80);- Parameters:
other- the date to compare actual todeltaInMilliseconds- the delta used for date comparison, expressed in milliseconds- Returns:
- this assertion object.
- Throws:
NullPointerException- ifDateparameter isnull.AssertionError- if the actualDateisnull.AssertionError- if the actualDateis not close to the given date by less than delta.
-
isCloseTo
Verifies that the actualDateis close to the givenInstantby less than delta (expressed in milliseconds), if the difference is equal to delta the assertion succeeds.One can use handy
TimeUnitto convert a duration in milliseconds, for example you can express a delta of 5 seconds withTimeUnit.SECONDS.toMillis(5).Note that using a
custom comparatorhas no effect on this assertion.Example:
Date date = new Date(); // assertions succeed assertThat(date).isCloseTo(date.toInstant().plusMillis(80), 80) .isCloseTo(date.toInstant().plusMillis(80), 100); // assertions fails assertThat(date).isCloseTo(date.toInstant().minusMillis(101), 100);- Parameters:
other- the Instant to compare actual todeltaInMilliseconds- the delta used for date comparison, expressed in milliseconds- Returns:
- this assertion object.
- Throws:
NullPointerException- ifInstantparameter isnull.AssertionError- if the actualDateisnull.AssertionError- if the actualDateis not close to the given Instant by less than delta.- Since:
- 3.19.0
-
isCloseTo
Same assertion asisCloseTo(Date, long)but given date is represented as String either with one of the supported defaults date format or a user custom date format (set with methodwithDateFormat(DateFormat)).User custom date format take precedence over the default ones.
Unless specified otherwise, beware that the default formats are expressed in the current local timezone.
Default date formats (expressed in the local time zone unless specified otherwise) are:
yyyy-MM-dd'T'HH:mm:ss.SSSX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ss.SSSyyyy-MM-dd HH:mm:ss.SSSyyyy-MM-dd'T'HH:mm:ssX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ssyyyy-MM-dd
Example of valid string date representations:
2003-04-26T03:01:02.758+00:002003-04-26T03:01:02.9992003-04-26 03:01:02.9992003-04-26T03:01:02+00:002003-04-26T13:01:022003-04-26
If you are getting an
IllegalArgumentExceptionwith "Unknown pattern character 'X'" message (some Android versions don't support it), you can explicitly specify the date format to use so that the default ones are bypassed.- Parameters:
dateAsString- the given Date represented as String in default or custom date format.deltaInMilliseconds- the delta used for date comparison, expressed in milliseconds- Returns:
- this assertion object.
- Throws:
NullPointerException- if dateAsString parameter isnull.AssertionError- if the actualDateisnull.AssertionError- if the actualDateis not close to the given date by less than delta.
-
hasTime
Verifies that the actualDatehas the same time as the given timestamp.Both time or timestamp express a number of milliseconds since January 1, 1970, 00:00:00 GMT.
Example:
assertThat(new Date(42)).hasTime(42);- Parameters:
timestamp- the timestamp to compare actual time to.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualDateisnull.AssertionError- if the actualDatetime is not equal to the given timestamp.- See Also:
-
hasSameTimeAs
Verifies that the actualDatehas the same time as the given date, useful to compareDateandTimestamp.Example:
Date date = new Date(); Timestamp timestamp = new Timestamp(date.getTime()); // Fail as date is not an instance of Timestamp assertThat(date).isEqualTo(timestamp); // Succeed as we compare date and timestamp time. assertThat(date).hasSameTimeAs(timestamp);- Parameters:
date- the date to compare actual time to.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualDateisnull.AssertionError- if the actualDatetime is not equal to the given date time.NullPointerException- ifDateparameter isnull.- See Also:
-
hasSameTimeAs
Verifies that the actualDaterepresents the same time as the given date inStringformat.It is the same assertion as
hasSameTimeAs(Date)but given date is represented as String either with one of the supported default date formats or a user custom date format (set with methodwithDateFormat(DateFormat)).User custom date format take precedence over the default ones.
Unless specified otherwise, beware that the default formats are expressed in the current local timezone.
Example:
Date date = parseDatetime("2003-04-26T12:00:00"); // assertion will pass assertThat(date).hasSameTimeAs("2003-04-26T12:00:00"); // assertion will fail assertThat(date).hasSameTimeAs("2003-04-26T12:00:01"); assertThat(date).hasSameTimeAs("2003-04-27T12:00:00")Defaults date format (expressed in the local time zone unless specified otherwise) are:
yyyy-MM-dd'T'HH:mm:ss.SSSX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ss.SSSyyyy-MM-dd HH:mm:ss.SSSyyyy-MM-dd'T'HH:mm:ssX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ssyyyy-MM-dd
Example of valid string date representations:
2003-04-26T03:01:02.758+00:002003-04-26T03:01:02.9992003-04-26 03:01:02.9992003-04-26T03:01:02+00:002003-04-26T13:01:022003-04-26
If you are getting an
IllegalArgumentExceptionwith "Unknown pattern character 'X'" message (some Android versions don't support it), you can explicitly specify the date format to use so that the default ones are bypassed.- Parameters:
dateAsString- the givenDaterepresented asStringin default or custom date format.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualDateisnull.NullPointerException- if given date as String isnull.AssertionError- if the actualDatetime is not equal to the time from date represented as String.AssertionError- if the given date as String could not be converted to a Date.
-
withDateFormat
Instead of using default date formats for the date String based Date assertions likeisEqualTo(String), AssertJ is going to use any date formats registered with one of these methods :withDateFormat(String)- this method
registerCustomDateFormat(java.text.DateFormat)registerCustomDateFormat(String)
Beware that :
- this will be the case for all future Date assertions in the test suite
- once a custom date format is registered, the default date formats are not used anymore
To revert to default format, call
useDefaultDateFormatsOnly()orwithDefaultDateFormatsOnly().- Parameters:
userCustomDateFormat- the new Date format used for String based Date assertions.- Returns:
- this assertion object.
-
withDateFormat
Instead of using default date formats for the date String based Date assertions likeisEqualTo(String), AssertJ is going to use any date formats registered with one of these methods :- this method
withDateFormat(java.text.DateFormat)registerCustomDateFormat(java.text.DateFormat)registerCustomDateFormat(String)
Beware that :
- this will be the case for all future Date assertions in the test suite
- once a custom date format is registered, the default date formats are not used anymore
To revert to default format, call
useDefaultDateFormatsOnly()orwithDefaultDateFormatsOnly().- Parameters:
userCustomDateFormatPattern- the new Date format string pattern used for String based Date assertions.- Returns:
- this assertion object.
-
setLenientDateParsing
public static void setLenientDateParsing(boolean lenientDateParsing) Instead of using default strict date/time parsing, it is possible to use lenient parsing mode for default date formats parser to interpret inputs that do not precisely match supported date formats (lenient parsing).With strict parsing, inputs must match exactly date/time formats.
Example:
To revert to default strict date parsing, callfinal Date date = Dates.parse("2001-02-03"); final Date dateTime = parseDatetime("2001-02-03T04:05:06"); final Date dateTimeWithMs = parseDatetimeWithMs("2001-02-03T04:05:06.700"); AbstractDateAssert.setLenientDateParsing(true); // assertions will pass assertThat(date).isEqualTo("2001-02-03"); assertThat(date).isEqualTo("2001-02-02T24:00:00"); assertThat(date).isEqualTo("2001-02-04T-24:00:00.000"); assertThat(dateTime).isEqualTo("2001-02-03T04:05:05.1000"); assertThat(dateTime).isEqualTo("2001-02-03T04:04:66"); assertThat(dateTimeWithMs).isEqualTo("2001-02-03T04:05:07.-300"); // assertions will fail assertThat(date).hasSameTimeAs("2001-02-04"); // different date assertThat(dateTime).hasSameTimeAs("2001-02-03 04:05:06"); // leniency does not help heresetLenientDateParsing(false).- Parameters:
lenientDateParsing- whether lenient parsing mode should be enabled or not
-
registerCustomDateFormat
Add the given date format to the ones used to parse date String in String based Date assertions likeisEqualTo(String).User date formats are used before default ones in the order they have been registered (first registered, first used).
AssertJ is gonna use any date formats registered with one of these methods :
withDateFormat(String)withDateFormat(java.text.DateFormat)- this method
registerCustomDateFormat(String)
Beware that AssertJ will use the newly registered format for all remaining Date assertions in the test suite
To revert to default formats only, call
useDefaultDateFormatsOnly()orwithDefaultDateFormatsOnly().Code examples:
Date date = ... // set to 2003 April the 26th assertThat(date).isEqualTo("2003-04-26"); try { // date with a custom format : failure since the default formats don't match. assertThat(date).isEqualTo("2003/04/26"); } catch (AssertionError e) { assertThat(e).hasMessage("Failed to parse 2003/04/26 with any of these date formats: " + "[yyyy-MM-dd'T'HH:mm:ss.SSS, yyyy-MM-dd'T'HH:mm:ss, yyyy-MM-dd]"); } // registering a custom date format to make the assertion pass registerCustomDateFormat(new SimpleDateFormat("yyyy/MM/dd")); // registerCustomDateFormat("yyyy/MM/dd") would work to. assertThat(date).isEqualTo("2003/04/26"); // the default formats are still available and should work assertThat(date).isEqualTo("2003-04-26");- Parameters:
userCustomDateFormat- the new Date format used for String based Date assertions.
-
registerCustomDateFormat
Add the given date format to the ones used to parse date String in String based Date assertions likeisEqualTo(String).User date formats are used before default ones in the order they have been registered (first registered, first used).
AssertJ is gonna use any date formats registered with one of these methods :
withDateFormat(String)withDateFormat(java.text.DateFormat)registerCustomDateFormat(java.text.DateFormat)- this method
Beware that AssertJ will use the newly registered format for all remaining Date assertions in the test suite
To revert to default formats only, call
useDefaultDateFormatsOnly()orwithDefaultDateFormatsOnly().Code examples:
Date date = ... // set to 2003 April the 26th assertThat(date).isEqualTo("2003-04-26"); try { // date with a custom format : failure since the default formats don't match. assertThat(date).isEqualTo("2003/04/26"); } catch (AssertionError e) { assertThat(e).hasMessage("Failed to parse 2003/04/26 with any of these date formats: " + "[yyyy-MM-dd'T'HH:mm:ss.SSS, yyyy-MM-dd'T'HH:mm:ss, yyyy-MM-dd]"); } // registering a custom date format to make the assertion pass registerCustomDateFormat("yyyy/MM/dd"); assertThat(date).isEqualTo("2003/04/26"); // the default formats are still available and should work assertThat(date).isEqualTo("2003-04-26");- Parameters:
userCustomDateFormatPattern- the new Date format pattern used for String based Date assertions.
-
useDefaultDateFormatsOnly
public static void useDefaultDateFormatsOnly()Remove all registered custom date formats => use only the defaults date formats to parse string as date.User custom date format take precedence over the default ones.
Unless specified otherwise, beware that the default formats are expressed in the current local timezone.
Defaults date format (expressed in the local time zone unless specified otherwise) are:
yyyy-MM-dd'T'HH:mm:ss.SSSX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ss.SSSyyyy-MM-dd HH:mm:ss.SSSyyyy-MM-dd'T'HH:mm:ssX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ssyyyy-MM-dd
Example of valid string date representations:
2003-04-26T03:01:02.758+00:002003-04-26T03:01:02.9992003-04-26 03:01:02.9992003-04-26T03:01:02+00:002003-04-26T13:01:022003-04-26
If you are getting an
IllegalArgumentExceptionwith "Unknown pattern character 'X'" message (some Android versions don't support it), you can explicitly specify the date format to use so that the default ones are bypassed. -
withDefaultDateFormatsOnly
Remove all registered custom date formats => use only the default date formats to parse string as date.User custom date format takes precedence over the default ones.
Unless specified otherwise, beware that the default formats are expressed in the current local timezone.
Defaults date format (expressed in the local time zone unless specified otherwise) are:
yyyy-MM-dd'T'HH:mm:ss.SSSX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ss.SSSyyyy-MM-dd HH:mm:ss.SSSyyyy-MM-dd'T'HH:mm:ssX(in ISO Time zone)yyyy-MM-dd'T'HH:mm:ssyyyy-MM-dd
Example of valid string date representations:
2003-04-26T03:01:02.758+00:002003-04-26T03:01:02.9992003-04-26 03:01:02.9992003-04-26T03:01:02+00:002003-04-26T13:01:022003-04-26
- Returns:
- this assertion
-
usingComparator
Description copied from class:AbstractAssertUse the given custom comparator instead of relying on actual type A equals method for incoming assertion checks.The custom comparator is bound to assertion instance, meaning that if a new assertion instance is created, the default comparison strategy will be used.
Examples :
// frodo and sam are instances of Character with Hobbit race (obviously :). // raceComparator implements Comparator<Character> assertThat(frodo).usingComparator(raceComparator).isEqualTo(sam);- Specified by:
usingComparatorin interfaceAssert<SELF extends AbstractDateAssert<SELF>,Date> - Overrides:
usingComparatorin classAbstractAssert<SELF extends AbstractDateAssert<SELF>,Date> - Parameters:
customComparator- the comparator to use for the incoming assertion checks.- Returns:
thisassertion object.
-
usingComparator
public SELF usingComparator(Comparator<? super Date> customComparator, String customComparatorDescription) Description copied from class:AbstractAssertUse the given custom comparator instead of relying on actual type A equals method for incoming assertion checks.The custom comparator is bound to the current assertion chain, meaning that if a new assertion instance is created, the default comparison strategy will be used.
Examples :
// frodo and sam are instances of Character with Hobbit race (obviously :). // raceComparator implements Comparator<Character> assertThat(frodo).usingComparator(raceComparator, "Hobbit Race Comparator").isEqualTo(sam);- Specified by:
usingComparatorin interfaceAssert<SELF extends AbstractDateAssert<SELF>,Date> - Overrides:
usingComparatorin classAbstractAssert<SELF extends AbstractDateAssert<SELF>,Date> - Parameters:
customComparator- the comparator to use for the incoming assertion checks.customComparatorDescription- comparator description to be used in assertion error messages- Returns:
thisassertion object.
-
usingDefaultComparator
Description copied from class:AbstractAssertRevert to standard comparison for the incoming assertion checks.This method should be used to disable a custom comparison strategy set by calling
usingComparator.- Specified by:
usingDefaultComparatorin interfaceAssert<SELF extends AbstractDateAssert<SELF>,Date> - Overrides:
usingDefaultComparatorin classAbstractAssert<SELF extends AbstractDateAssert<SELF>,Date> - Returns:
thisassertion object.
-
isAfterOrEqualTo(String)