Best Assertj code snippet using org.assertj.core.api.AbstractZonedDateTimeAssert.areEqualIgnoringNanos
Source:AbstractZonedDateTimeAssert.java
...335 public SELF isEqualToIgnoringNanos(ZonedDateTime other) {336 Objects.instance().assertNotNull(info, actual);337 assertDateTimeParameterIsNotNull(other);338 ZonedDateTime otherInActualTimeZone = sameInstantInActualTimeZone(other);339 if (!areEqualIgnoringNanos(actual, otherInActualTimeZone)) {340 throw Failures.instance().failure(info, shouldBeEqualIgnoringNanos(actual, otherInActualTimeZone));341 }342 return myself;343 }344 /**345 * Verifies that actual and given {@link ZonedDateTime} have same year, month, day, hour and minute fields (second and346 * nanosecond fields are ignored in comparison).347 * <p>348 * Note that given {@link ZonedDateTime} is converted in the actual's {@link java.time.ZoneId} before comparison.349 * <p>350 * Assertion can fail with ZonedDateTimes in same chronological second time window, e.g :351 * <p>352 * 2000-01-01T00:<b>01:00</b>.000 and 2000-01-01T00:<b>00:59</b>.000.353 * <p>354 * Assertion fails as minute fields differ even if time difference is only 1ns.355 * <p>356 * Code example :357 * <pre><code class='java'> // successful assertions358 * ZonedDateTime dateTime1 = ZonedDateTime.of(2000, 1, 1, 23, 50, 0, 0);359 * ZonedDateTime dateTime2 = ZonedDateTime.of(2000, 1, 1, 23, 50, 10, 456);360 * assertThat(dateTime1).isEqualToIgnoringSeconds(dateTime2);361 *362 * // failing assertions (even if time difference is only 1ns)363 * ZonedDateTime dateTimeA = ZonedDateTime.of(2000, 1, 1, 23, 50, 00, 0);364 * ZonedDateTime dateTimeB = ZonedDateTime.of(2000, 1, 1, 23, 49, 59, 999999999);365 * assertThat(dateTimeA).isEqualToIgnoringSeconds(dateTimeB);</code></pre>366 *367 * @param other the given {@link ZonedDateTime}.368 * @return this assertion object.369 * @throws AssertionError if the actual {@code ZonedDateTime} is {@code null}.370 * @throws IllegalArgumentException if other {@code ZonedDateTime} is {@code null}.371 * @throws AssertionError if the actual {@code ZonedDateTime} is are not equal with second and nanosecond fields372 * ignored.373 */374 public SELF isEqualToIgnoringSeconds(ZonedDateTime other) {375 Objects.instance().assertNotNull(info, actual);376 assertDateTimeParameterIsNotNull(other);377 ZonedDateTime otherInActualTimeZone = sameInstantInActualTimeZone(other);378 if (!areEqualIgnoringSeconds(actual, otherInActualTimeZone)) {379 throw Failures.instance().failure(info, shouldBeEqualIgnoringSeconds(actual, otherInActualTimeZone));380 }381 return myself;382 }383 /**384 * Verifies that actual and given {@code ZonedDateTime} have same year, month, day and hour fields (minute, second and385 * nanosecond fields are ignored in comparison).386 * <p>387 * Note that given {@link ZonedDateTime} is converted in the actual's {@link java.time.ZoneId} before comparison.388 * <p>389 * Assertion can fail with dateTimes in same chronological second time window, e.g :390 * <p>391 * 2000-01-01T<b>01:00</b>:00.000 and 2000-01-01T<b>00:59:59</b>.000.392 * <p>393 * Time difference is only 1s but hour fields differ.394 * <p>395 * Code example :396 * <pre><code class='java'> // successful assertions397 * ZonedDateTime dateTime1 = ZonedDateTime.of(2000, 1, 1, 23, 50, 0, 0);398 * ZonedDateTime dateTime2 = ZonedDateTime.of(2000, 1, 1, 23, 00, 2, 7);399 * assertThat(dateTime1).isEqualToIgnoringMinutes(dateTime2);400 *401 * // failing assertions (even if time difference is only 1ms)402 * ZonedDateTime dateTimeA = ZonedDateTime.of(2000, 1, 1, 01, 00, 00, 000);403 * ZonedDateTime dateTimeB = ZonedDateTime.of(2000, 1, 1, 00, 59, 59, 999);404 * assertThat(dateTimeA).isEqualToIgnoringMinutes(dateTimeB);</code></pre>405 *406 * @param other the given {@link ZonedDateTime}.407 * @return this assertion object.408 * @throws AssertionError if the actual {@code ZonedDateTime} is {@code null}.409 * @throws IllegalArgumentException if other {@code ZonedDateTime} is {@code null}.410 * @throws AssertionError if the actual {@code ZonedDateTime} is are not equal ignoring minute, second and nanosecond411 * fields.412 */413 public SELF isEqualToIgnoringMinutes(ZonedDateTime other) {414 Objects.instance().assertNotNull(info, actual);415 assertDateTimeParameterIsNotNull(other);416 ZonedDateTime otherInActualTimeZone = sameInstantInActualTimeZone(other);417 if (!areEqualIgnoringMinutes(actual, otherInActualTimeZone)) {418 throw Failures.instance().failure(info, shouldBeEqualIgnoringMinutes(actual, otherInActualTimeZone));419 }420 return myself;421 }422 /**423 * Verifies that actual and given {@code ZonedDateTime} have same year, month and day fields (hour, minute, second and424 * nanosecond fields are ignored in comparison).425 * <p>426 * Note that given {@link ZonedDateTime} is converted in the actual's {@link java.time.ZoneId} before comparison.427 * <p>428 * Assertion can fail with dateTimes in same chronological minute time window, e.g :429 * <p>430 * 2000-01-<b>01T23:59</b>:00.000 and 2000-01-02T<b>00:00</b>:00.000.431 * <p>432 * Time difference is only 1min but day fields differ.433 * <p>434 * Code example :435 * <pre><code class='java'> // successful assertions436 * ZonedDateTime dateTime1 = ZonedDateTime.of(2000, 1, 1, 23, 59, 59, 999, ZoneId.systemDefault());437 * ZonedDateTime dateTime2 = ZonedDateTime.of(2000, 1, 1, 00, 00, 00, 000, ZoneId.systemDefault());438 * assertThat(dateTime1).isEqualToIgnoringHours(dateTime2);439 *440 * // failing assertions (even if time difference is only 1ms)441 * ZonedDateTime dateTimeA = ZonedDateTime.of(2000, 1, 2, 00, 00, 00, 000, ZoneId.systemDefault());442 * ZonedDateTime dateTimeB = ZonedDateTime.of(2000, 1, 1, 23, 59, 59, 999, ZoneId.systemDefault());443 * assertThat(dateTimeA).isEqualToIgnoringHours(dateTimeB);</code></pre>444 *445 * @param other the given {@link ZonedDateTime}.446 * @return this assertion object.447 * @throws AssertionError if the actual {@code ZonedDateTime} is {@code null}.448 * @throws IllegalArgumentException if other {@code ZonedDateTime} is {@code null}.449 * @throws AssertionError if the actual {@code ZonedDateTime} is are not equal with second and nanosecond fields450 * ignored.451 */452 public SELF isEqualToIgnoringHours(ZonedDateTime other) {453 Objects.instance().assertNotNull(info, actual);454 assertDateTimeParameterIsNotNull(other);455 ZonedDateTime otherInActualTimeZone = sameInstantInActualTimeZone(other);456 if (!haveSameYearMonthAndDayOfMonth(actual, otherInActualTimeZone)) {457 throw Failures.instance().failure(info, shouldBeEqualIgnoringHours(actual, otherInActualTimeZone));458 }459 return myself;460 }461 /**462 * Verifies that the actual {@link ZonedDateTime} is equal to the given one according to the comparator in use.463 * <p>464 * <b>Breaking change</b>: since 3.15.0 the default comparator uses {@link ChronoZonedDateTime#timeLineOrder()} which only465 * compares the underlying instant and not the chronology. The underlying comparison is equivalent to comparing the epoch-second and nano-of-second.<br>466 * This behaviour can be overridden by {@link AbstractZonedDateTimeAssert#usingComparator(Comparator)}.467 * <p>468 * Example :469 * <pre><code class='java'> ZonedDateTime firstOfJanuary2000InUTC = ZonedDateTime.parse("2000-01-01T00:00:00Z");470 *471 * // both assertions succeed, the second one because the comparison based on the instant they are referring to472 * // 2000-01-01T01:00:00+01:00 = 2000-01-01T00:00:00 in UTC473 * assertThat(firstOfJanuary2000InUTC).isEqualTo(parse("2000-01-01T00:00:00Z"))474 * .isEqualTo(parse("2000-01-01T01:00:00+01:00"));475 *476 * // assertions fail477 * assertThat(firstOfJanuary2000InUTC).isEqualTo(parse("1999-01-01T01:00:00Z"));478 * // fails as the comparator compares the offsets479 * assertThat(firstOfJanuary2000InUTC).usingComparator(ZonedDateTime::compareTo)480 * .isEqualTo(parse("2000-01-01T01:00:00+01:00"));</code></pre>481 *482 * @param expected the given value to compare the actual value to.483 * @return {@code this} assertion object.484 * @throws AssertionError if the actual {@code ZonedDateTime} is not equal to the {@link ZonedDateTime} according485 * to the comparator in use.486 */487 @Override488 public SELF isEqualTo(Object expected) {489 if (actual == null || expected == null) {490 super.isEqualTo(expected);491 } else {492 comparables.assertEqual(info, actual, expected);493 }494 return myself;495 }496 /**497 * Same assertion as {@link #isEqualTo(Object)} but the {@link ZonedDateTime} is built from given String which must follow498 * <a href="http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_DATE_TIME"499 * >ISO date-time format</a> to allow calling {@link ZonedDateTime#parse(CharSequence, DateTimeFormatter)} method.500 * <p>501 * <b>Breaking change</b>: since 3.15.0 the default comparator uses {@link ChronoZonedDateTime#timeLineOrder()} which only502 * compares the underlying instant and not the chronology. The underlying comparison is equivalent to comparing the epoch-second and nano-of-second.<br>503 * This behaviour can be overridden by {@link AbstractZonedDateTimeAssert#usingComparator(Comparator)}.504 * <p>505 * Example :506 * <pre><code class='java'> ZonedDateTime firstOfJanuary2000InUTC = ZonedDateTime.parse("2000-01-01T00:00:00Z");507 *508 * // both assertions succeed, the second one because the comparison based on the instant they are referring to509 * // 2000-01-01T01:00:00+01:00 = 2000-01-01T00:00:00 in UTC510 * assertThat(firstOfJanuary2000InUTC).isEqualTo("2000-01-01T00:00:00Z")511 * .isEqualTo("2000-01-01T01:00:00+01:00");512 *513 * // assertions fail514 * assertThat(firstOfJanuary2000InUTC).isEqualTo("1999-01-01T01:00:00Z");515 * // fails as the comparator compares the offsets516 * assertThat(firstOfJanuary2000InUTC).usingComparator(ZonedDateTime::compareTo)517 * .isEqualTo("2000-01-01T01:00:00+01:00");</code></pre>518 *519 * @param dateTimeAsString String representing a {@link ZonedDateTime}.520 * @return this assertion object.521 * @throws AssertionError if the actual {@code ZonedDateTime} is {@code null}.522 * @throws IllegalArgumentException if given String is null or can't be converted to a {@link ZonedDateTime}.523 * @throws AssertionError if the actual {@code ZonedDateTime} is not equal to the {@link ZonedDateTime} built from the given String.524 */525 public SELF isEqualTo(String dateTimeAsString) {526 assertDateTimeAsStringParameterIsNotNull(dateTimeAsString);527 return isEqualTo(parse(dateTimeAsString));528 }529 /**530 * Verifies that the actual value is not equal to the given one according to the comparator in use.531 * <p>532 * <b>Breaking change</b>: since 3.15.0 the default comparator uses {@link ChronoZonedDateTime#timeLineOrder()} which only533 * compares the underlying instant and not the chronology. The underlying comparison is equivalent to comparing the epoch-second and nano-of-second.<br>534 * This behaviour can be overridden by {@link AbstractZonedDateTimeAssert#usingComparator(Comparator)}.535 * <p>536 * Example :537 * <pre><code class='java'> // assertions succeed538 * assertThat(parse("2000-01-01T00:00:00Z")).isNotEqualTo(parse("2020-01-01T00:00:00Z"));539 * // even though they refer to the same instant, succeeds as the ZonedDateTime comparator checks the offsets540 * assertThat(parse("2000-01-01T00:00:00Z")).usingComparator(ZonedDateTime::compareTo)541 * .isNotEqualTo(parse("2000-01-01T02:00:00+02:00"));542 *543 * // assertions fail544 * assertThat(parse("2000-01-01T00:00:00Z")).isNotEqualTo(parse("2000-01-01T00:00:00Z"));545 * // fails because the default comparator only checks the instant and they refer to the same546 * assertThat(parse("2000-01-01T00:00:00Z")).isNotEqualTo(parse("2000-01-01T02:00:00+02:00"));</code></pre>547 *548 * @param expected the given value to compare the actual value to.549 * @return {@code this} assertion object.550 * @throws AssertionError if the actual {@code ZonedDateTime} is equal to the {@link ZonedDateTime} according551 * to the comparator in use.552 */553 @Override554 public SELF isNotEqualTo(Object expected) {555 if (actual == null || expected == null) {556 super.isNotEqualTo(expected);557 } else {558 comparables.assertNotEqual(info, actual, expected);559 }560 return myself;561 }562 /**563 * Same assertion as {@code #isNotEqualTo(Object)} but the {@link ZonedDateTime} is built from given String which must follow564 * <a href="http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_DATE_TIME">ISO date-time format</a>565 * to allow calling {@link ZonedDateTime#parse(CharSequence, DateTimeFormatter)} method.566 * <p>567 * <b>Breaking change</b>: since 3.15.0 the default comparator uses {@link ChronoZonedDateTime#timeLineOrder()} which only568 * compares the underlying instant and not the chronology. The underlying comparison is equivalent to comparing the epoch-second and nano-of-second.<br>569 * This behaviour can be overridden by {@link AbstractZonedDateTimeAssert#usingComparator(Comparator)}.570 * <p>571 * Example :572 * <pre><code class='java'> // assertions succeed573 * assertThat(parse("2000-01-01T00:00:00Z")).isNotEqualTo("2020-01-01T00:00:00Z");574 * // even though they refer to the same instant, succeeds as the ZonedDateTime comparator checks the offsets575 * assertThat(parse("2000-01-01T00:00:00Z")).usingComparator(ZonedDateTime::compareTo)576 * .isNotEqualTo("2000-01-01T02:00:00+02:00");577 *578 * // assertions fail579 * assertThat(parse("2000-01-01T00:00:00Z")).isNotEqualTo("2000-01-01T00:00:00Z");580 * // fails because the default comparator only checks the instant and they refer to the same581 * assertThat(parse("2000-01-01T00:00:00Z")).isNotEqualTo("2000-01-01T02:00:00+02:00");</code></pre>582 *583 * @param dateTimeAsString String representing a {@link ZonedDateTime}.584 * @return this assertion object.585 * @throws AssertionError if the actual {@code ZonedDateTime} is {@code null}.586 * @throws IllegalArgumentException if given String is null or can't be converted to a {@link ZonedDateTime}.587 * @throws AssertionError if the actual {@code ZonedDateTime} is equal to the {@link ZonedDateTime} built from given {@link String}.588 */589 public SELF isNotEqualTo(String dateTimeAsString) {590 assertDateTimeAsStringParameterIsNotNull(dateTimeAsString);591 return isNotEqualTo(parse(dateTimeAsString));592 }593 /**594 * Verifies that the actual {@link ZonedDateTime} is equal to one of the given {@link ZonedDateTime} <b>in the actual595 * ZonedDateTime's {@link java.time.ZoneId}</b>.596 * <p>597 * Example :598 * <pre><code class='java'> assertThat(parse("2000-01-01T00:00:00Z")).isIn(parse("1999-12-31T23:59:59Z"),599 * parse("2000-01-01T00:00:00Z"));</code></pre>600 *601 * @param expected the given {@link ZonedDateTime}s to compare the actual value to.602 * @return {@code this} assertion object.603 * @throws AssertionError if the actual {@code ZonedDateTime} is {@code null}.604 * @throws AssertionError if the actual {@code ZonedDateTime} is not in the given {@link ZonedDateTime}s.605 */606 public SELF isIn(ZonedDateTime... expected) {607 return isIn((Object[]) changeToActualTimeZone(expected));608 }609 /**610 * Same assertion as {@link #isIn(ZonedDateTime...)} but the {@link ZonedDateTime} are built from given String, which611 * must follow <a612 * href="http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_DATE_TIME"613 * >ISO date-time format</a> to allow calling {@link ZonedDateTime#parse(CharSequence, DateTimeFormatter)} method.614 * <p>615 * Note that the {@link ZonedDateTime}s created from the given Strings are built in the {@link java.time.ZoneId} of616 * the {@link ZonedDateTime} to check..617 * <p>618 * Example :619 * <pre><code class='java'> // use String based representation of LocalDateTime620 * assertThat(parse("2000-01-01T00:00:00Z")).isIn("1999-12-31T23:59:59Z",621 * "2000-01-01T00:00:00Z");</code></pre>622 *623 * @param dateTimesAsString String array representing {@link ZonedDateTime}s.624 * @return this assertion object.625 * @throws AssertionError if the actual {@code ZonedDateTime} is {@code null}.626 * @throws IllegalArgumentException if given String is null or can't be converted to a {@link ZonedDateTime}.627 * @throws AssertionError if the actual {@code ZonedDateTime} is not in the {@link ZonedDateTime}s built from given628 * Strings.629 */630 public SELF isIn(String... dateTimesAsString) {631 checkIsNotNullAndNotEmpty(dateTimesAsString);632 return isIn(convertToDateTimeArray(dateTimesAsString));633 }634 /**635 * Verifies that the actual {@link ZonedDateTime} is equal to one of the given {@link ZonedDateTime} <b>in the actual636 * ZonedDateTime's {@link java.time.ZoneId}</b>.637 * <p>638 * Example :639 * <pre><code class='java'> assertThat(parse("2000-01-01T00:00:00Z")).isNotIn(parse("1999-12-31T23:59:59Z"),640 * parse("2000-01-02T00:00:00Z"));</code></pre>641 *642 * @param expected the given {@link ZonedDateTime}s to compare the actual value to.643 * @return {@code this} assertion object.644 * @throws AssertionError if the actual {@code ZonedDateTime} is {@code null}.645 * @throws AssertionError if the actual {@code ZonedDateTime} is not in the given {@link ZonedDateTime}s.646 */647 public SELF isNotIn(ZonedDateTime... expected) {648 return isNotIn((Object[]) changeToActualTimeZone(expected));649 }650 /**651 * Same assertion as {@link #isNotIn(ZonedDateTime...)} but the {@link ZonedDateTime} is built from given String,652 * which must follow <a653 * href="http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_DATE_TIME"654 * >ISO date-time format</a> to allow calling {@link ZonedDateTime#parse(CharSequence, DateTimeFormatter)} method.655 * <p>656 * Note that the {@link ZonedDateTime}s created from the given Strings are built in the {@link java.time.ZoneId} of657 * the {@link ZonedDateTime} to check..658 * <p>659 * Example :660 * <pre><code class='java'> // use String based representation of ZonedDateTime661 * assertThat(parse("2000-01-01T00:00:00Z")).isNotIn("1999-12-31T23:59:59Z",662 * "2000-01-02T00:00:00Z");</code></pre>663 *664 * @param dateTimesAsString String array representing {@link ZonedDateTime}s.665 * @return this assertion object.666 * @throws AssertionError if the actual {@code ZonedDateTime} is {@code null}.667 * @throws IllegalArgumentException if given String is null or can't be converted to a {@link ZonedDateTime}.668 * @throws AssertionError if the actual {@code ZonedDateTime} is not equal to the {@link ZonedDateTime} built from669 * given String.670 */671 public SELF isNotIn(String... dateTimesAsString) {672 checkIsNotNullAndNotEmpty(dateTimesAsString);673 return isNotIn(convertToDateTimeArray(dateTimesAsString));674 }675 /**676 * Verifies that the actual {@link ZonedDateTime} is in the [start, end] period (start and end included) according to677 * the comparator in use.678 * <p>679 * <b>Breaking change</b>: since 3.15.0 the default comparator uses {@link ChronoZonedDateTime#timeLineOrder()} which only680 * compares the underlying instant and not the chronology. The underlying comparison is equivalent to comparing the epoch-second and nano-of-second.<br>681 * This behaviour can be overridden by {@link AbstractZonedDateTimeAssert#usingComparator(Comparator)}.682 * <p>683 * Example:684 * <pre><code class='java'> ZonedDateTime zonedDateTime = ZonedDateTime.now();685 *686 * // assertions succeed:687 * assertThat(zonedDateTime).isBetween(zonedDateTime.minusSeconds(1), zonedDateTime.plusSeconds(1))688 * .isBetween(zonedDateTime, zonedDateTime.plusSeconds(1))689 * .isBetween(zonedDateTime.minusSeconds(1), zonedDateTime)690 * .isBetween(zonedDateTime, zonedDateTime);691 * // succeeds with default comparator which compares the point in time692 * assertThat(parse("2010-01-01T00:00:00Z")).isBetween(parse("2010-01-01T01:00:00+01:00"),693 * parse("2010-01-01T01:00:00+01:00"));694 *695 * // assertions fail:696 * assertThat(zonedDateTime).isBetween(zonedDateTime.minusSeconds(10), zonedDateTime.minusSeconds(1));697 * assertThat(zonedDateTime).isBetween(zonedDateTime.plusSeconds(1), zonedDateTime.plusSeconds(10));698 * // fails because the comparator checks the offsets are the same699 * assertThat(parse("2010-01-01T00:00:00Z")).usingComparator(ZonedDateTime::compareTo)700 * .isBetween(parse("2010-01-01T01:00:00+01:00"),701 * parse("2010-01-01T01:00:00+01:00"));</code></pre>702 *703 * @param startInclusive the start value (inclusive), expected not to be null.704 * @param endInclusive the end value (inclusive), expected not to be null.705 * @return this assertion object.706 * @throws AssertionError if the actual value is {@code null}.707 * @throws NullPointerException if start value is {@code null}.708 * @throws NullPointerException if end value is {@code null}.709 * @throws AssertionError if the actual value is not in [start, end] period.710 *711 * @since 3.7.1712 */713 public SELF isBetween(ZonedDateTime startInclusive, ZonedDateTime endInclusive) {714 comparables.assertIsBetween(info, actual, startInclusive, endInclusive, true, true);715 return myself;716 }717 /**718 * Same assertion as {@link #isBetween(ZonedDateTime, ZonedDateTime)} but here you pass {@link ZonedDateTime} String representations719 * which must follow <a href="http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_ZONED_DATE_TIME">ISO ZonedDateTime format</a>720 * to allow calling {@link ZonedDateTime#parse(CharSequence)} method.721 * <p>722 * <b>Breaking change</b>: since 3.15.0 the default comparator uses {@link ChronoZonedDateTime#timeLineOrder()} which only723 * compares the underlying instant and not the chronology. The underlying comparison is equivalent to comparing the epoch-second and nano-of-second.<br>724 * This behaviour can be overridden by {@link AbstractZonedDateTimeAssert#usingComparator(Comparator)}.725 * <p>726 * Example:727 * <pre><code class='java'> ZonedDateTime firstOfJanuary2000 = ZonedDateTime.parse("2000-01-01T00:00:00Z");728 *729 * // assertions succeed:730 * assertThat(firstOfJanuary2000).isBetween("1999-12-31T23:59:59Z", "2000-01-01T00:00:01Z")731 * .isBetween("2000-01-01T00:00:00Z", "2000-01-01T00:00:01Z")732 * .isBetween("1999-12-31T23:59:59Z", "2000-01-01T00:00:00Z")733 * .isBetween("2000-01-01T00:00:00Z", "2000-01-01T00:00:00Z")734 * // same instant as firstOfJanuary2000 but on a different offset735 * .isBetween("2000-01-01T01:00:00+01:00", "2000-01-01T01:00:00+01:00");736 *737 * // assertion fails:738 * assertThat(firstOfJanuary2000).isBetween("1999-01-01T00:00:01Z", "1999-12-31T23:59:59Z");</code></pre>739 *740 * @param startInclusive the start value (inclusive), expected not to be null.741 * @param endInclusive the end value (inclusive), expected not to be null.742 * @return this assertion object.743 *744 * @throws AssertionError if the actual value is {@code null}.745 * @throws NullPointerException if start value is {@code null}.746 * @throws NullPointerException if end value is {@code null}.747 * @throws DateTimeParseException if any of the given String can't be converted to a {@link ZonedDateTime}.748 * @throws AssertionError if the actual value is not in [start, end] period.749 *750 * @since 3.7.1751 */752 public SELF isBetween(String startInclusive, String endInclusive) {753 return isBetween(parse(startInclusive), parse(endInclusive));754 }755 /**756 * Verifies that the actual {@link ZonedDateTime} is in the ]start, end[ period (start and end excluded) according to757 * the comparator in use.758 * <p>759 * <b>Breaking change</b>: since 3.15.0 the default comparator uses {@link ChronoZonedDateTime#timeLineOrder()} which only760 * compares the underlying instant and not the chronology. The underlying comparison is equivalent to comparing the epoch-second and nano-of-second.<br>761 * This behaviour can be overridden by {@link AbstractZonedDateTimeAssert#usingComparator(Comparator)}.762 * <p>763 * Example:764 * <pre><code class='java'> ZonedDateTime zonedDateTime = ZonedDateTime.now();765 *766 * // assertions succeed:767 * assertThat(zonedDateTime).isStrictlyBetween(zonedDateTime.minusSeconds(1), zonedDateTime.plusSeconds(1));768 * // succeeds with a different comparator even though the end value refers to the same instant as the actual769 * assertThat(parse("2010-01-01T12:00:00Z")).usingComparator(ZonedDateTime::compareTo)770 * .isStrictlyBetween(parse("2010-01-01T12:59:59+01:00"),771 * parse("2010-01-01T13:00:00+01:00"));772 *773 * // assertions fail:774 * assertThat(zonedDateTime).isStrictlyBetween(zonedDateTime.minusSeconds(10), zonedDateTime.minusSeconds(1));775 * assertThat(zonedDateTime).isStrictlyBetween(zonedDateTime.plusSeconds(1), zonedDateTime.plusSeconds(10));776 * assertThat(zonedDateTime).isStrictlyBetween(zonedDateTime, zonedDateTime.plusSeconds(1));777 * assertThat(zonedDateTime).isStrictlyBetween(zonedDateTime.minusSeconds(1), zonedDateTime);778 * // fails with default comparator since the end value refers to the same instant as the actual779 * assertThat(parse("2010-01-01T12:00:00Z")).isStrictlyBetween(parse("2010-01-01T12:59:59+01:00"),780 * parse("2010-01-01T13:00:00+01:00"));</code></pre>781 *782 * @param startExclusive the start value (exclusive), expected not to be null.783 * @param endExclusive the end value (exclusive), expected not to be null.784 * @return this assertion object.785 * @throws AssertionError if the actual value is {@code null}.786 * @throws NullPointerException if start value is {@code null}.787 * @throws NullPointerException if end value is {@code null}.788 * @throws AssertionError if the actual value is not in ]start, end[ period.789 *790 * @since 3.7.1791 */792 public SELF isStrictlyBetween(ZonedDateTime startExclusive, ZonedDateTime endExclusive) {793 comparables.assertIsBetween(info, actual, startExclusive, endExclusive, false, false);794 return myself;795 }796 /**797 * Same assertion as {@link #isStrictlyBetween(ZonedDateTime, ZonedDateTime)} but here you pass {@link ZonedDateTime} String representations798 * which must follow <a href="http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_ZONED_DATE_TIME">ISO ZonedDateTime format</a>799 * to allow calling {@link ZonedDateTime#parse(CharSequence)} method.800 * <p>801 * <b>Breaking change</b>: since 3.15.0 the default comparator uses {@link ChronoZonedDateTime#timeLineOrder()} which only802 * compares the underlying instant and not the chronology. The underlying comparison is equivalent to comparing the epoch-second and nano-of-second.<br>803 * This behaviour can be overridden by {@link AbstractZonedDateTimeAssert#usingComparator(Comparator)}.804 * <p>805 * Example:806 * <pre><code class='java'> ZonedDateTime firstOfJanuary2000 = ZonedDateTime.parse("2000-01-01T00:00:00Z");807 *808 * // assertions succeed:809 * assertThat(firstOfJanuary2000).isStrictlyBetween("1999-12-31T23:59:59Z", "2000-01-01T00:00:01Z")810 * // succeeds with a different comparator even though the end value refers to the same instant as the actual811 * .usingComparator(ZonedDateTime::compareTo)812 * .isStrictlyBetween("1999-12-31T23:59:59Z", "2000-01-01T01:00:00+01:00");813 *814 * // assertions fail:815 * assertThat(firstOfJanuary2000).isStrictlyBetween("1999-01-01T00:00:01Z", "1999-12-31T23:59:59Z");816 * assertThat(firstOfJanuary2000).isStrictlyBetween("2000-01-01T00:00:00Z", "2000-01-01T00:00:01Z");817 * assertThat(firstOfJanuary2000).isStrictlyBetween("1999-12-31T23:59:59Z", "2000-01-01T00:00:00Z");818 * // fails with default comparator since the end value refers to the same instant as the actual819 * assertThat(parse("2010-01-01T12:00:00Z")).isStrictlyBetween("2010-01-01T12:59:59+01:00", "2010-01-01T13:00:00+01:00");</code></pre>820 *821 * @param startExclusive the start value (exclusive), expected not to be null.822 * @param endExclusive the end value (exclusive), expected not to be null.823 * @return this assertion object.824 *825 * @throws AssertionError if the actual value is {@code null}.826 * @throws NullPointerException if start value is {@code null}.827 * @throws NullPointerException if end value is {@code null}.828 * @throws DateTimeParseException if any of the given String can't be converted to a {@link ZonedDateTime}.829 * @throws AssertionError if the actual value is not in ]start, end[ period.830 *831 * @since 3.7.1832 */833 public SELF isStrictlyBetween(String startExclusive, String endExclusive) {834 return isStrictlyBetween(parse(startExclusive), parse(endExclusive));835 }836 /** {@inheritDoc} */837 @Override838 @CheckReturnValue839 public SELF usingDefaultComparator() {840 SELF self = super.usingDefaultComparator();841 self.comparables = buildDefaultComparables();842 return self;843 }844 private Comparables buildDefaultComparables() {845 ChronoZonedDateTimeByInstantComparator defaultComparator = ChronoZonedDateTimeByInstantComparator.getInstance();846 return new Comparables(new ComparatorBasedComparisonStrategy(defaultComparator, defaultComparator.description()));847 }848 private ZonedDateTime[] convertToDateTimeArray(String... dateTimesAsString) {849 ZonedDateTime[] dates = new ZonedDateTime[dateTimesAsString.length];850 for (int i = 0; i < dateTimesAsString.length; i++) {851 dates[i] = parse(dateTimesAsString[i]);852 }853 return dates;854 }855 private ZonedDateTime[] changeToActualTimeZone(ZonedDateTime... dateTimes) {856 ZonedDateTime[] dates = new ZonedDateTime[dateTimes.length];857 for (int i = 0; i < dateTimes.length; i++) {858 dates[i] = sameInstantInActualTimeZone(dateTimes[i]);859 }860 return dates;861 }862 private void checkIsNotNullAndNotEmpty(Object[] values) {863 checkArgument(values != null, "The given ZonedDateTime array should not be null");864 checkArgument(values.length > 0, "The given ZonedDateTime array should not be empty");865 }866 /**867 * Obtains an instance of {@link ZonedDateTime} from a string representation in ISO date format.868 *869 * @param dateTimeAsString the string to parse870 * @return the parsed {@link ZonedDateTime}871 */872 @Override873 protected ZonedDateTime parse(String dateTimeAsString) {874 return ZonedDateTime.parse(dateTimeAsString, DateTimeFormatter.ISO_DATE_TIME);875 }876 private ZonedDateTime sameInstantInActualTimeZone(ZonedDateTime zonedDateTime) {877 if (zonedDateTime == null) return null; // nothing to convert in actual's TZ878 if (actual == null) return zonedDateTime; // no actual => let's keep zonedDateTime as it is.879 return zonedDateTime.withZoneSameInstant(actual.getZone());880 }881 /**882 * Check that the {@link ZonedDateTime} string representation to compare actual {@link ZonedDateTime} to is not null,883 * otherwise throws a {@link IllegalArgumentException} with an explicit message884 *885 * @param dateTimeAsString String representing the ZonedDateTime to compare actual with886 * @throws IllegalArgumentException with an explicit message if the given {@link String} is null887 */888 private static void assertDateTimeAsStringParameterIsNotNull(String dateTimeAsString) {889 checkArgument(dateTimeAsString != null,890 "The String representing the ZonedDateTime to compare actual with should not be null");891 }892 /**893 * Returns true if both datetime are in the same year, month and day of month, hour, minute and second, false894 * otherwise.895 *896 * @param actual the actual datetime. expected not be null897 * @param other the other datetime. expected not be null898 * @return true if both datetime are in the same year, month and day of month, hour, minute and second, false899 * otherwise.900 */901 private static boolean areEqualIgnoringNanos(ZonedDateTime actual, ZonedDateTime other) {902 return areEqualIgnoringSeconds(actual, other) && actual.getSecond() == other.getSecond();903 }904 /**905 * Returns true if both datetime are in the same year, month, day of month, hour and minute, false otherwise.906 *907 * @param actual the actual datetime. expected not be null908 * @param other the other datetime. expected not be null909 * @return true if both datetime are in the same year, month, day of month, hour and minute, false otherwise.910 */911 private static boolean areEqualIgnoringSeconds(ZonedDateTime actual, ZonedDateTime other) {912 return areEqualIgnoringMinutes(actual, other) && actual.getMinute() == other.getMinute();913 }914 /**915 * Returns true if both datetime are in the same year, month, day of month and hour, false otherwise....
areEqualIgnoringNanos
Using AI Code Generation
1import static java.time.ZoneOffset.UTC;2import static java.time.ZonedDateTime.of;3import static java.time.format.DateTimeFormatter.ISO_OFFSET_DATE_TIME;4import static org.assertj.core.api.Assertions.assertThat;5import java.time.ZonedDateTime;6import org.junit.jupiter.api.Test;7public class ZonedDateTimeAssert_areEqualIgnoringNanos_Test {8 public void test() {9 ZonedDateTime zonedDateTime1 = of(2018, 3, 18, 13, 30, 0, 0, UTC);10 ZonedDateTime zonedDateTime2 = ZonedDateTime.parse("2018-03-18T13:30:00Z", ISO_OFFSET_DATE_TIME);11 assertThat(zonedDateTime1).isEqualToIgnoringNanos(zonedDateTime2);12 }13}14Example 2: assertThat(ZonedDateTime) isEqualToIgnoringNanos(ZonedDateTime)15assertThat(ZonedDateTime) isEqualToIgnoringNanos(ZonedDateTime) example in JUnit 516package com.baeldung.zoneddatetime;17import static java.time.ZoneOffset.UTC;18import static java.time.ZonedDateTime.of;19import static java.time.format.DateTimeFormatter.ISO_OFFSET_DATE_TIME;20import static org.assertj.core.api.Assertions.assertThat;21import java.time.ZonedDateTime;22import org.junit.jupiter.api.Test;23public class ZonedDateTimeAssert_areEqualIgnoringNanos_Test {24 public void test() {25 ZonedDateTime zonedDateTime1 = of(2018, 3, 18, 13, 30, 0, 0, UTC);26 ZonedDateTime zonedDateTime2 = ZonedDateTime.parse("2018-03-18T13:30:00Z", ISO_OFFSET_DATE_TIME);27 assertThat(zonedDateTime1).isEqualToIgnoringNanos(zonedDateTime2);28 }29}30Example 3: assertThat(ZonedDateTime) isEqualToIgnoringNanos(String)31assertThat(ZonedDateTime) isEqualToIgnoringNanos(String) example in JUnit
areEqualIgnoringNanos
Using AI Code Generation
1import static java.time.ZoneOffset.UTC;2import static java.time.ZonedDateTime.of;3import static org.assertj.core.api.Assertions.assertThat;4import java.time.ZonedDateTime;5import org.junit.jupiter.api.Test;6public class ZonedDateTimeAssert_isEqualToIgnoringNanos_Test {7 public void test_isEqualToIgnoringNanos_assertion() {8 ZonedDateTime dateTime1 = of(2000, 1, 5, 3, 0, 5, 0, UTC);9 ZonedDateTime dateTime2 = of(2000, 1, 5, 3, 0, 5, 1, UTC);10 assertThat(dateTime1).isEqualToIgnoringNanos(dateTime2);11 }12}13import static java.time.ZoneOffset.UTC;14import static java.time.ZonedDateTime.of;15import static org.assertj.core.api.Assertions.assertThat;16import java.time.ZonedDateTime;17import org.junit.jupiter.api.Test;18public class ZonedDateTimeAssert_isEqualToIgnoringSeconds_Test {19 public void test_isEqualToIgnoringSeconds_assertion() {20 ZonedDateTime dateTime1 = of(2000, 1, 5, 3, 0, 5, 0, UTC);21 ZonedDateTime dateTime2 = of(2000, 1, 5, 3, 0, 6, 0, UTC);22 assertThat(dateTime1).isEqualToIgnoringSeconds(dateTime2);23 }24}
areEqualIgnoringNanos
Using AI Code Generation
1ZonedDateTime date1 = ZonedDateTime.of(2018, 1, 1, 0, 0, 0, 0, ZoneId.of("UTC"));2ZonedDateTime date2 = ZonedDateTime.of(2018, 1, 1, 0, 0, 0, 1000, ZoneId.of("UTC"));3assertThat(date1).isEqualToIgnoringNanos(date2);4Example 2: assertThat(ZonedDateTime).isNotEqualToIgnoringNanos(ZonedDateTime)5package com.journaldev.assertj;6import static org.assertj.core.api.Assertions.assertThat;7import static org.assertj.core.api.Assertions.assertThatExceptionOfType;8import java.time.ZoneId;9import java.time.ZonedDateTime;10import org.junit.jupiter.api.Test;11public class AssertJZonedDateTimeTest {12 public void testAssertJZonedDateTime() {13 ZonedDateTime date1 = ZonedDateTime.of(2018, 1, 1, 0, 0, 0, 0, ZoneId.of("UTC"));14 ZonedDateTime date2 = ZonedDateTime.of(2018, 1, 1, 0, 0, 0, 1000, ZoneId.of("UTC"));15 assertThat(date1).isNotEqualToIgnoringNanos(date2);16 }17}18Example 3: assertThat(ZonedDateTime).isEqualToIgnoringSeconds(ZonedDateTime)
areEqualIgnoringNanos
Using AI Code Generation
1ZonedDateTime zdt1 = ZonedDateTime.of(2015, 1, 1, 0, 0, 0, 0, ZoneId.of("UTC"));2ZonedDateTime zdt2 = ZonedDateTime.of(2015, 1, 1, 0, 0, 0, 1, ZoneId.of("UTC"));3assertThat(zdt1).isNotEqualTo(zdt2)4 .isEqualToIgnoringNanos(zdt2);5assertThat(zdt1).isNotEqualToIgnoringNanos(zdt2);6ZonedDateTime zdt3 = ZonedDateTime.of(2015, 1, 1, 0, 0, 0, 1, ZoneId.of("UTC"));7ZonedDateTime zdt4 = ZonedDateTime.of(2015, 1, 1, 0, 0, 0, 1, ZoneId.of("UTC"));8assertThat(zdt3).isEqualToIgnoringNanos(zdt4);9assertThat(zdt3).isNotEqualToIgnoringNanos(zdt4);10ZonedDateTime zdt5 = ZonedDateTime.of(2015, 1, 1, 0, 0, 0, 1, ZoneId.of("UTC"));11ZonedDateTime zdt6 = ZonedDateTime.of(2015, 1, 1, 0, 0, 0, 2, ZoneId.of("UTC"));12assertThat(zdt5).isNotEqualToIgnoringNanos(zdt6);13assertThat(zdt5).isEqualToIgnoringNanos(zdt6);14ZonedDateTime zdt7 = ZonedDateTime.of(2015, 1, 1, 0, 0, 0, 1, ZoneId.of("UTC"));15ZonedDateTime zdt8 = ZonedDateTime.of(2015, 1,
areEqualIgnoringNanos
Using AI Code Generation
1import java.time.ZonedDateTime;2import java.time.ZoneId;3import java.time.ZoneOffset;4import org.junit.Test;5import static org.assertj.core.api.Assertions.assertThat;6public class ZonedDateTimeAssert_isEqualTo_Test {7 public void isEqualTo_should_pass() {8 ZonedDateTime dateTime = ZonedDateTime.of(2000, 1, 1, 0, 0, 0, 0, ZoneId.of("UTC"));9 assertThat(dateTime).isEqualTo("2000-01-01T00:00:00Z");10 }11 public void isEqualTo_should_pass_with_nanos_ignored() {12 ZonedDateTime dateTime = ZonedDateTime.of(2000, 1, 1, 0, 0, 0, 1, ZoneId.of("UTC"));13 assertThat(dateTime).isEqualTo("2000-01-01T00:00:00Z");14 assertThat(dateTime).isEqualTo("2000-01-01T00:00:00.000000001Z");15 assertThat(dateTime).isEqualTo("2000-01-01T00:00:00.000000001+00:00");16 assertThat(dateTime).isEqualTo("2000-01-01T00:00:00.000000001+00");17 assertThat(dateTime).isEqualTo("2000-01-01T00:00:00.000000001+0000");18 assertThat(dateTime).isEqualTo("2000-01-01T00:00:00.000000001+000000");19 assertThat(dateTime).isEqualTo("2000-01-01T00:00:00.000000001+01:00");20 assertThat(dateTime).isEqualTo("2000-01-01T00:00:00.000000001+01");21 assertThat(dateTime).isEqualTo("2000-01-01T00:00:00.000000001+0100");22 assertThat(dateTime).isEqualTo("2000-01-01T00:00:00.000000001+010000");23 assertThat(dateTime).isEqualTo("2000-01-01T00:00:00.000000001+01:30");24 assertThat(dateTime).isEqualTo("2000-01-01T00:00:00.000000001+01:30:00");25 assertThat(dateTime).isEqualTo("2000-01-01T00:00:00.000000001
areEqualIgnoringNanos
Using AI Code Generation
1import org.assertj.core.api.Assertions.assertThat;2import java.time.ZonedDateTime;3import java.time.ZoneId;4import java.time.LocalDateTime;5import java.time.Month;6public class ZonedDateTimeAssert_areEqualIgnoringNanos_Test {7 public static void main(String[] args) {8 ZonedDateTime actual = ZonedDateTime.of(LocalDateTime.of(2018, Month.JANUARY, 1, 0, 0, 0), ZoneId.of("UTC"));9 ZonedDateTime expected = ZonedDateTime.of(LocalDateTime.of(2018, Month.JANUARY, 1, 0, 0, 0), ZoneId.of("UTC"));10 assertThat(actual).isNotNull();11 assertThat(actual).isNotSameAs(expected);12 assertThat(actual).isEqualTo(expected);13 assertThat(actual).isNotEqualTo(ZonedDateTime.of(LocalDateTime.of(2018, Month.JANUARY, 1, 0, 0, 1), ZoneId.of("UTC")));14 assertThat(actual).isNotEqualTo(ZonedDateTime.of(LocalDateTime.of(2018, Month.JANUARY, 1, 0, 0, 0), ZoneId.of("UTC-1")));15 assertThat(actual).isNotEqualTo(ZonedDateTime.of(LocalDateTime.of(2018, Month.JANUARY, 1, 0, 0, 0), ZoneId.of("UTC")).plusNanos(1));16 assertThat(actual).isEqualToIgnoringNanos(ZonedDateTime.of(LocalDateTime.of(2018, Month.JANUARY, 1, 0, 0, 0), ZoneId.of("UTC")).plusNanos(1));17 assertThat(actual).isNotEqualTo(ZonedDateTime.of(LocalDateTime.of(2018, Month.JANUARY, 1, 0, 0, 0), ZoneId.of("UTC")).plusNanos(1));18 assertThat(actual).isNotEqualTo(ZonedDateTime.of(LocalDateTime.of(2018, Month.JANUARY, 1, 0, 0, 0), ZoneId.of("UTC")).minusNanos(1));19 assertThat(actual).isEqualToIgnoringNanos(ZonedDateTime.of(LocalDateTime.of(2018, Month.JANUARY, 1, 0,
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!