How to use parse method of org.assertj.core.api.AbstractLocalDateTimeAssert class

Best Assertj code snippet using org.assertj.core.api.AbstractLocalDateTimeAssert.parse

Source:AbstractLocalDateTimeAssert.java Github

copy

Full Screen

...62 * <p>63 * This behaviour can be overridden by {@link AbstractLocalDateTimeAssert#usingComparator(Comparator)}.64 * <p>65 * Example :66 * <pre><code class='java'> assertThat(parse("2000-01-01T23:59:59")).isBefore(parse("2000-01-02T00:00:00"));</code></pre>67 *68 * @param other the given {@link LocalDateTime}.69 * @return this assertion object.70 * @throws AssertionError if the actual {@code LocalDateTime} is {@code null}.71 * @throws IllegalArgumentException if other {@code LocalDateTime} is {@code null}.72 * @throws AssertionError if the actual {@code LocalDateTime} is not strictly before the given one.73 */74 public SELF isBefore(LocalDateTime other) {75 assertLocalDateTimeParameterIsNotNull(other);76 comparables.assertIsBefore(info, actual, other);77 return myself;78 }79 /**80 * Same assertion as {@link #isBefore(LocalDateTime)} but the {@link LocalDateTime} is built from given String, which81 * must follow <a href=82 * "http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_LOCAL_DATE_TIME"83 * >ISO LocalDateTime format</a> to allow calling {@link LocalDateTime#parse(CharSequence)} method.84 * <p>85 * Example :86 * <pre><code class='java'> // use String in comparison to avoid writing the code to perform the conversion87 * assertThat(parse("2000-01-01T23:59:59")).isBefore("2000-01-02T00:00:00");</code></pre>88 *89 * @param localDateTimeAsString String representing a {@link LocalDateTime}.90 * @return this assertion object.91 * @throws AssertionError if the actual {@code LocalDateTime} is {@code null}.92 * @throws IllegalArgumentException if given String is null or can't be converted to a {@link LocalDateTime}.93 * @throws AssertionError if the actual {@code LocalDateTime} is not strictly before the {@link LocalDateTime} built94 * from given String.95 */96 public SELF isBefore(String localDateTimeAsString) {97 assertLocalDateTimeAsStringParameterIsNotNull(localDateTimeAsString);98 return isBefore(parse(localDateTimeAsString));99 }100 /**101 * Verifies that the actual {@code LocalDateTime} is before or equals to the given one according to the {@link ChronoLocalDateTime#timeLineOrder()} comparator102 * which is consistent with {@link LocalDateTime#isBefore(ChronoLocalDateTime)}.103 * <p>104 * {@link ChronoLocalDateTime#timeLineOrder()} compares {@code LocalDateTime} in time-line order <b>ignoring the chronology</b>, this is equivalent to comparing the epoch-day and nano-of-day.105 * <p>106 * This behaviour can be overridden by {@link AbstractLocalDateTimeAssert#usingComparator(Comparator)}.107 * <p>108 * Example :109 * <pre><code class='java'> assertThat(parse("2000-01-01T23:59:59")).isBeforeOrEqualTo(parse("2000-01-01T23:59:59"))110 * .isBeforeOrEqualTo(parse("2000-01-02T00:00:00"));</code></pre>111 *112 * @param other the given {@link LocalDateTime}.113 * @return this assertion object.114 * @throws AssertionError if the actual {@code LocalDateTime} is {@code null}.115 * @throws IllegalArgumentException if other {@code LocalDateTime} is {@code null}.116 * @throws AssertionError if the actual {@code LocalDateTime} is not before or equals to the given one.117 */118 public SELF isBeforeOrEqualTo(LocalDateTime other) {119 assertLocalDateTimeParameterIsNotNull(other);120 comparables.assertIsBeforeOrEqualTo(info, actual, other);121 return myself;122 }123 /**124 * Same assertion as {@link #isBeforeOrEqualTo(LocalDateTime)} but the {@link LocalDateTime} is built from given125 * String, which must follow <a href=126 * "http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_LOCAL_DATE_TIME"127 * >ISO LocalDateTime format</a> to allow calling {@link LocalDateTime#parse(CharSequence)} method.128 * <p>129 * Example :130 * <pre><code class='java'> // use String in comparison to avoid conversion131 * assertThat(parse("2000-01-01T23:59:59")).isBeforeOrEqualTo("2000-01-01T23:59:59")132 * .isBeforeOrEqualTo("2000-01-02T00:00:00");</code></pre>133 *134 * @param localDateTimeAsString String representing a {@link LocalDateTime}.135 * @return this assertion object.136 * @throws AssertionError if the actual {@code LocalDateTime} is {@code null}.137 * @throws IllegalArgumentException if given String is null or can't be converted to a {@link LocalDateTime}.138 * @throws AssertionError if the actual {@code LocalDateTime} is not before or equals to the {@link LocalDateTime}139 * built from given String.140 */141 public SELF isBeforeOrEqualTo(String localDateTimeAsString) {142 assertLocalDateTimeAsStringParameterIsNotNull(localDateTimeAsString);143 return isBeforeOrEqualTo(parse(localDateTimeAsString));144 }145 /**146 * Verifies that the actual {@code LocalDateTime} is after or equals to the given one according to the {@link ChronoLocalDateTime#timeLineOrder()} comparator147 * which is consistent with {@link LocalDateTime#isAfter(ChronoLocalDateTime)}.148 * <p>149 * {@link ChronoLocalDateTime#timeLineOrder()} compares {@code LocalDateTime} in time-line order <b>ignoring the chronology</b>, this is equivalent to comparing the epoch-day and nano-of-day.150 * <p>151 * This behaviour can be overridden by {@link AbstractLocalDateTimeAssert#usingComparator(Comparator)}.152 * <p>153 * Example :154 * <pre><code class='java'> assertThat(parse("2000-01-01T00:00:00")).isAfterOrEqualTo(parse("2000-01-01T00:00:00"))155 * .isAfterOrEqualTo(parse("1999-12-31T23:59:59"));</code></pre>156 *157 * @param other the given {@link LocalDateTime}.158 * @return this assertion object.159 * @throws AssertionError if the actual {@code LocalDateTime} is {@code null}.160 * @throws IllegalArgumentException if other {@code LocalDateTime} is {@code null}.161 * @throws AssertionError if the actual {@code LocalDateTime} is not after or equals to the given one.162 */163 public SELF isAfterOrEqualTo(LocalDateTime other) {164 assertLocalDateTimeParameterIsNotNull(other);165 comparables.assertIsAfterOrEqualTo(info, actual, other);166 return myself;167 }168 /**169 * Same assertion as {@link #isAfterOrEqualTo(LocalDateTime)} but the {@link LocalDateTime} is built from given170 * String, which must follow <a href=171 * "http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_LOCAL_DATE_TIME"172 * >ISO LocalDateTime format</a> to allow calling {@link LocalDateTime#parse(CharSequence)} method.173 * <p>174 * Example :175 * <pre><code class='java'> // use String in comparison to avoid conversion176 * assertThat(parse("2000-01-01T00:00:00")).isAfterOrEqualTo("2000-01-01T00:00:00")177 * .isAfterOrEqualTo("1999-12-31T23:59:59");</code></pre>178 *179 * @param localDateTimeAsString String representing a {@link LocalDateTime}.180 * @return this assertion object.181 * @throws AssertionError if the actual {@code LocalDateTime} is {@code null}.182 * @throws IllegalArgumentException if given String is null or can't be converted to a {@link LocalDateTime}.183 * @throws AssertionError if the actual {@code LocalDateTime} is not after or equals to the {@link LocalDateTime}184 * built from given String.185 */186 public SELF isAfterOrEqualTo(String localDateTimeAsString) {187 assertLocalDateTimeAsStringParameterIsNotNull(localDateTimeAsString);188 return isAfterOrEqualTo(parse(localDateTimeAsString));189 }190 /**191 * Verifies that the actual {@code LocalDateTime} is <b>strictly</b> after the given one according to the {@link ChronoLocalDateTime#timeLineOrder()} comparator192 * which is consistent with {@link LocalDateTime#isAfter(ChronoLocalDateTime)}.193 * <p>194 * {@link ChronoLocalDateTime#timeLineOrder()} compares {@code LocalDateTime} in time-line order <b>ignoring the chronology</b>, this is equivalent to comparing the epoch-day and nano-of-day.195 * <p>196 * This behaviour can be overridden by {@link AbstractLocalDateTimeAssert#usingComparator(Comparator)}.197 * <p>198 * Example :199 * <pre><code class='java'> assertThat(parse("2000-01-01T00:00:00")).isAfter(parse("1999-12-31T23:59:59"));</code></pre>200 *201 * @param other the given {@link LocalDateTime}.202 * @return this assertion object.203 * @throws AssertionError if the actual {@code LocalDateTime} is {@code null}.204 * @throws IllegalArgumentException if other {@code LocalDateTime} is {@code null}.205 * @throws AssertionError if the actual {@code LocalDateTime} is not strictly after the given one.206 */207 public SELF isAfter(LocalDateTime other) {208 assertLocalDateTimeParameterIsNotNull(other);209 comparables.assertIsAfter(info, actual, other);210 return myself;211 }212 /**213 * Same assertion as {@link #isAfter(LocalDateTime)} but the {@link LocalDateTime} is built from given a String that214 * must follow <a href=215 * "http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_LOCAL_DATE_TIME"216 * >ISO LocalDateTime format</a> to allow calling {@link LocalDateTime#parse(CharSequence)} method.217 * <p>218 * Example :219 * <pre><code class='java'> // use String in comparison to avoid conversion220 * assertThat(parse("2000-01-01T00:00:00")).isAfter("1999-12-31T23:59:59");</code></pre>221 *222 * @param localDateTimeAsString String representing a {@link LocalDateTime}.223 * @return this assertion object.224 * @throws AssertionError if the actual {@code LocalDateTime} is {@code null}.225 * @throws IllegalArgumentException if given String is null or can't be converted to a {@link LocalDateTime}.226 * @throws AssertionError if the actual {@code LocalDateTime} is not strictly after the {@link LocalDateTime} built227 * from given String.228 */229 public SELF isAfter(String localDateTimeAsString) {230 assertLocalDateTimeAsStringParameterIsNotNull(localDateTimeAsString);231 return isAfter(parse(localDateTimeAsString));232 }233 /**234 * Verifies that the actual {@code LocalDateTime} is equal to the given one according to the {@link ChronoLocalDateTime#timeLineOrder()} comparator235 * which is consistent with {@link LocalDateTime#isEqual(ChronoLocalDateTime)}.236 * <p>237 * {@link ChronoLocalDateTime#timeLineOrder()} compares {@code LocalDateTime} in time-line order <b>ignoring the chronology</b>, this is equivalent to comparing the epoch-day and nano-of-day.238 * <p>239 * This behaviour can be overridden by {@link AbstractLocalDateTimeAssert#usingComparator(Comparator)}.240 * <p>241 * Example :242 * <pre><code class='java'> assertThat(parse("2000-01-01T00:00:00")).isEqualTo(parse("2000-01-01T00:00:00"));</code></pre>243 *244 * @param other the given {@link LocalDateTime}.245 * @return this assertion object.246 * @throws AssertionError if the actual {@code LocalDateTime} is {@code null}.247 * @throws AssertionError if the actual {@code LocalDateTime} differs from the given {@code LocalDateTime}248 * according to the comparator in use.249 */250 @Override251 public SELF isEqualTo(Object other) {252 if (actual == null || other == null) {253 super.isEqualTo(other);254 } else {255 comparables.assertEqual(info, actual, other);256 }257 return myself;258 }259 /**260 * Same assertion as {@link #isEqualTo(Object)} (where Object is expected to be {@link LocalDateTime}) but here you261 * pass {@link LocalDateTime} String representation that must follow <a href=262 * "http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_LOCAL_DATE_TIME"263 * >ISO LocalDateTime format</a> to allow calling {@link LocalDateTime#parse(CharSequence)} method.264 * <p>265 * Example :266 * <pre><code class='java'> // use String in comparison to avoid writing the code to perform the conversion267 * assertThat(parse("2000-01-01T00:00:00")).isEqualTo("2000-01-01T00:00:00");</code></pre>268 *269 * @param dateTimeAsString String representing a {@link LocalDateTime}.270 * @return this assertion object.271 * @throws AssertionError if the actual {@code LocalDateTime} is {@code null}.272 * @throws IllegalArgumentException if given String is null or can't be converted to a {@link LocalDateTime}.273 * @throws AssertionError if the actual {@code LocalDateTime} is not equal to the {@link LocalDateTime} built from274 * given String.275 */276 public SELF isEqualTo(String dateTimeAsString) {277 assertLocalDateTimeAsStringParameterIsNotNull(dateTimeAsString);278 return isEqualTo(parse(dateTimeAsString));279 }280 /**281 * Verifies that the actual {@code LocalDateTime} is not equal to the given one according to the {@link ChronoLocalDateTime#timeLineOrder()} comparator282 * which is consistent with {@link LocalDateTime#isEqual(ChronoLocalDateTime)}.283 * <p>284 * {@link ChronoLocalDateTime#timeLineOrder()} compares {@code LocalDateTime} in time-line order <b>ignoring the chronology</b>, this is equivalent to comparing the epoch-day and nano-of-day.285 * <p>286 * This behaviour can be overridden by {@link AbstractLocalDateTimeAssert#usingComparator(Comparator)}.287 * <p>288 * Example :289 * <pre><code class='java'> assertThat(parse("2000-01-01T00:00:00")).isEqualTo(parse("2000-01-01T00:00:00"));</code></pre>290 *291 * @param other the given value to compare the actual value to.292 * @return this assertion object.293 * @throws AssertionError if the actual {@code LocalDateTime} is {@code null}.294 * @throws AssertionError if the actual {@code LocalDateTime} equals to the given {@code LocalDateTime}295 * according to the comparator in use.296 */297 @Override298 public SELF isNotEqualTo(Object other) {299 if (actual == null || other == null) {300 super.isNotEqualTo(other);301 } else {302 comparables.assertNotEqual(info, actual, other);303 }304 return myself;305 }306 /**307 * Same assertion as {@link #isNotEqualTo(Object)} (where Object is expected to be {@link LocalDateTime}) but here you308 * pass {@link LocalDateTime} String representation that must follow <a href=309 * "http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_LOCAL_DATE_TIME"310 * >ISO LocalDateTime format</a> to allow calling {@link LocalDateTime#parse(CharSequence)} method.311 * <p>312 * Example :313 * <pre><code class='java'> // use String in comparison to avoid writing the code to perform the conversion314 * assertThat(parse("2000-01-01T00:00:00")).isNotEqualTo("2000-01-15T00:00:00");</code></pre>315 *316 * @param dateTimeAsString String representing a {@link LocalDateTime}.317 * @return this assertion object.318 * @throws AssertionError if the actual {@code LocalDateTime} is {@code null}.319 * @throws IllegalArgumentException if given String is null or can't be converted to a {@link LocalDateTime}.320 * @throws AssertionError if the actual {@code LocalDateTime} is equal to the {@link LocalDateTime} built from given321 * String.322 */323 public SELF isNotEqualTo(String dateTimeAsString) {324 assertLocalDateTimeAsStringParameterIsNotNull(dateTimeAsString);325 return isNotEqualTo(parse(dateTimeAsString));326 }327 /**328 * Same assertion as {@link #isIn(Object...)} (where Objects are expected to be {@link LocalDateTime}) but here you329 * pass {@link LocalDateTime} String representations that must follow <a href=330 * "http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_LOCAL_DATE_TIME"331 * >ISO LocalDateTime format</a> to allow calling {@link LocalDateTime#parse(CharSequence)} method.332 * <p>333 * Example :334 * <pre><code class='java'> // use String based representation of LocalDateTime335 * assertThat(parse("2000-01-01T00:00:00")).isIn("1999-12-31T00:00:00", "2000-01-01T00:00:00");</code></pre>336 *337 * @param dateTimesAsString String array representing {@link LocalDateTime}s.338 * @return this assertion object.339 * @throws AssertionError if the actual {@code LocalDateTime} is {@code null}.340 * @throws IllegalArgumentException if given String is null or can't be converted to a {@link LocalDateTime}.341 * @throws AssertionError if the actual {@code LocalDateTime} is not in the {@link LocalDateTime}s built from given342 * Strings.343 */344 public SELF isIn(String... dateTimesAsString) {345 checkIsNotNullAndNotEmpty(dateTimesAsString);346 return isIn(convertToLocalDateTimeArray(dateTimesAsString));347 }348 /**349 * Same assertion as {@link #isNotIn(Object...)} (where Objects are expected to be {@link LocalDateTime}) but here you350 * pass {@link LocalDateTime} String representations that must follow <a href=351 * "http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_LOCAL_DATE_TIME"352 * >ISO LocalDateTime format</a> to allow calling {@link LocalDateTime#parse(CharSequence)} method.353 * <p>354 * Example :355 * <pre><code class='java'> // use String based representation of LocalDateTime356 * assertThat(parse("2000-01-01T00:00:00")).isNotIn("1999-12-31T00:00:00", "2000-01-02T00:00:00");</code></pre>357 *358 * @param dateTimesAsString Array of String representing a {@link LocalDateTime}.359 * @return this assertion object.360 * @throws AssertionError if the actual {@code LocalDateTime} is {@code null}.361 * @throws IllegalArgumentException if given String is null or can't be converted to a {@link LocalDateTime}.362 * @throws AssertionError if the actual {@code LocalDateTime} is in the {@link LocalDateTime}s built from given363 * Strings.364 */365 public SELF isNotIn(String... dateTimesAsString) {366 checkIsNotNullAndNotEmpty(dateTimesAsString);367 return isNotIn(convertToLocalDateTimeArray(dateTimesAsString));368 }369 /**370 * Verifies that the actual {@link LocalDateTime} is close to the current date and time on the UTC timezone,371 * according to the given {@link TemporalUnitOffset}.372 * You can build the offset parameter using {@link Assertions#within(long, TemporalUnit)} or {@link Assertions#byLessThan(long, TemporalUnit)}.373 * <p>374 * If the difference is equal to the offset, the assertion succeeds.375 * <p>376 * Example:377 * <pre><code class='java'> LocalDateTime actual = LocalDateTime.now(Clock.systemUTC());378 *379 * // assertion will pass as if executed less than one second after actual was built380 * assertThat(actual).isCloseToUtcNow(within(1, ChronoUnit.SECONDS));381 *382 * // assertion will fail383 * assertThat(actual.plusSeconds(2)).isCloseToUtcNow(within(1, ChronoUnit.SECONDS));</code></pre>384 *385 * @param offset The offset used for comparison386 * @return this assertion object387 * @throws NullPointerException if {@code offset} parameter is {@code null}.388 * @throws AssertionError if the actual {@code LocalDateTime} is {@code null}.389 * @throws AssertionError if the actual {@code LocalDateTime} is not close to the current time by less than the given offset.390 */391 public SELF isCloseToUtcNow(TemporalUnitOffset offset) {392 return isCloseTo(now(systemUTC()), offset);393 }394 /** {@inheritDoc} */395 @Override396 @CheckReturnValue397 public SELF usingDefaultComparator() {398 SELF self = super.usingDefaultComparator();399 self.comparables = buildDefaultComparables();400 return self;401 }402 private Comparables buildDefaultComparables() {403 ChronoLocalDateTimeComparator defaultComparator = ChronoLocalDateTimeComparator.getInstance();404 return new Comparables(new ComparatorBasedComparisonStrategy(defaultComparator, defaultComparator.description()));405 }406 private static Object[] convertToLocalDateTimeArray(String... dateTimesAsString) {407 return Arrays.stream(dateTimesAsString)408 .map(LocalDateTime::parse)409 .toArray();410 }411 private void checkIsNotNullAndNotEmpty(Object[] values) {412 checkArgument(values != null, "The given LocalDateTime array should not be null");413 checkArgument(values.length > 0, "The given LocalDateTime array should not be empty");414 }415 /**416 * Check that the {@link LocalDateTime} string representation to compare actual {@link LocalDateTime} to is not null,417 * otherwise throws a {@link IllegalArgumentException} with an explicit message418 *419 * @param localDateTimeAsString String representing the {@link LocalDateTime} to compare actual with420 * @throws IllegalArgumentException with an explicit message if the given {@link String} is null421 */422 private static void assertLocalDateTimeAsStringParameterIsNotNull(String localDateTimeAsString) {423 checkArgument(localDateTimeAsString != null,424 "The String representing the LocalDateTime to compare actual with should not be null");425 }426 /**427 * Check that the {@link LocalDateTime} to compare actual {@link LocalDateTime} to is not null, in that case throws a428 * {@link IllegalArgumentException} with an explicit message429 *430 * @param other the {@link LocalDateTime} to check431 * @throws IllegalArgumentException with an explicit message if the given {@link LocalDateTime} is null432 */433 private static void assertLocalDateTimeParameterIsNotNull(LocalDateTime other) {434 checkArgument(other != null, "The LocalDateTime to compare actual with should not be null");435 }436 /**437 * Verifies that actual and given {@code LocalDateTime} have same year, month, day, hour, minute and second fields,438 * (nanosecond fields are ignored in comparison).439 * <p>440 * Assertion can fail with localDateTimes in same chronological nanosecond time window, e.g :441 * <p>442 * 2000-01-01T00:00:<b>01.000000000</b> and 2000-01-01T00:00:<b>00.999999999</b>.443 * <p>444 * Assertion fails as second fields differ even if time difference is only 1ns.445 * <p>446 * Code example :447 * <pre><code class='java'> // successful assertions448 * LocalDateTime localDateTime1 = LocalDateTime.of(2000, 1, 1, 0, 0, 1, 0);449 * LocalDateTime localDateTime2 = LocalDateTime.of(2000, 1, 1, 0, 0, 1, 456);450 * assertThat(localDateTime1).isEqualToIgnoringNanos(localDateTime2);451 *452 * // failing assertions (even if time difference is only 1ms)453 * LocalDateTime localDateTimeA = LocalDateTime.of(2000, 1, 1, 0, 0, 1, 0);454 * LocalDateTime localDateTimeB = LocalDateTime.of(2000, 1, 1, 0, 0, 0, 999999999);455 * assertThat(localDateTimeA).isEqualToIgnoringNanos(localDateTimeB);</code></pre>456 *457 * @param other the given {@link LocalDateTime}.458 * @return this assertion object.459 * @throws AssertionError if the actual {@code LocalDateTime} is {@code null}.460 * @throws IllegalArgumentException if other {@code LocalDateTime} is {@code null}.461 * @throws AssertionError if the actual {@code LocalDateTime} is are not equal with nanoseconds ignored.462 */463 public SELF isEqualToIgnoringNanos(LocalDateTime other) {464 Objects.instance().assertNotNull(info, actual);465 assertLocalDateTimeParameterIsNotNull(other);466 if (!areEqualIgnoringNanos(actual, other)) {467 throw Failures.instance().failure(info, shouldBeEqualIgnoringNanos(actual, other));468 }469 return myself;470 }471 /**472 * Verifies that actual and given {@link LocalDateTime} have same year, month, day, hour and minute fields (second and473 * nanosecond fields are ignored in comparison).474 * <p>475 * Assertion can fail with LocalDateTimes in same chronological second time window, e.g :476 * <p>477 * 2000-01-01T00:<b>01:00</b>.000 and 2000-01-01T00:<b>00:59</b>.000.478 * <p>479 * Assertion fails as minute fields differ even if time difference is only 1s.480 * <p>481 * Code example :482 * <pre><code class='java'> // successful assertions483 * LocalDateTime localDateTime1 = LocalDateTime.of(2000, 1, 1, 23, 50, 0, 0);484 * LocalDateTime localDateTime2 = LocalDateTime.of(2000, 1, 1, 23, 50, 10, 456);485 * assertThat(localDateTime1).isEqualToIgnoringSeconds(localDateTime2);486 *487 * // failing assertions (even if time difference is only 1ms)488 * LocalDateTime localDateTimeA = LocalDateTime.of(2000, 1, 1, 23, 50, 00, 000);489 * LocalDateTime localDateTimeB = LocalDateTime.of(2000, 1, 1, 23, 49, 59, 999);490 * assertThat(localDateTimeA).isEqualToIgnoringSeconds(localDateTimeB);</code></pre>491 *492 * @param other the given {@link LocalDateTime}.493 * @return this assertion object.494 * @throws AssertionError if the actual {@code LocalDateTime} is {@code null}.495 * @throws IllegalArgumentException if other {@code LocalDateTime} is {@code null}.496 * @throws AssertionError if the actual {@code LocalDateTime} is are not equal with second and nanosecond fields497 * ignored.498 */499 public SELF isEqualToIgnoringSeconds(LocalDateTime other) {500 Objects.instance().assertNotNull(info, actual);501 assertLocalDateTimeParameterIsNotNull(other);502 if (!areEqualIgnoringSeconds(actual, other)) {503 throw Failures.instance().failure(info, shouldBeEqualIgnoringSeconds(actual, other));504 }505 return myself;506 }507 /**508 * Verifies that actual and given {@code LocalDateTime} have same year, month, day and hour fields (minute, second and509 * nanosecond fields are ignored in comparison).510 * <p>511 * Assertion can fail with localDateTimes in same chronological second time window, e.g :512 * <p>513 * 2000-01-01T<b>01:00</b>:00.000 and 2000-01-01T<b>00:59:59</b>.000.514 * <p>515 * Time difference is only 1s but hour fields differ.516 * <p>517 * Code example :518 * <pre><code class='java'> // successful assertions519 * LocalDateTime localDateTime1 = LocalDateTime.of(2000, 1, 1, 23, 50, 0, 0);520 * LocalDateTime localDateTime2 = LocalDateTime.of(2000, 1, 1, 23, 00, 2, 7);521 * assertThat(localDateTime1).isEqualToIgnoringMinutes(localDateTime2);522 *523 * // failing assertions (even if time difference is only 1ms)524 * LocalDateTime localDateTimeA = LocalDateTime.of(2000, 1, 1, 01, 00, 00, 000);525 * LocalDateTime localDateTimeB = LocalDateTime.of(2000, 1, 1, 00, 59, 59, 999);526 * assertThat(localDateTimeA).isEqualToIgnoringMinutes(localDateTimeB);</code></pre>527 *528 * @param other the given {@link LocalDateTime}.529 * @return this assertion object.530 * @throws AssertionError if the actual {@code LocalDateTime} is {@code null}.531 * @throws IllegalArgumentException if other {@code LocalDateTime} is {@code null}.532 * @throws AssertionError if the actual {@code LocalDateTime} is are not equal ignoring minute, second and nanosecond533 * fields.534 */535 public SELF isEqualToIgnoringMinutes(LocalDateTime other) {536 Objects.instance().assertNotNull(info, actual);537 assertLocalDateTimeParameterIsNotNull(other);538 if (!areEqualIgnoringMinutes(actual, other)) {539 throw Failures.instance().failure(info, shouldBeEqualIgnoringMinutes(actual, other));540 }541 return myself;542 }543 /**544 * Verifies that actual and given {@code LocalDateTime} have same year, month and day fields (hour, minute, second and545 * nanosecond fields are ignored in comparison).546 * <p>547 * Assertion can fail with localDateTimes in same chronological minute time window, e.g :548 * <p>549 * 2000-01-<b>01T23:59</b>:00.000 and 2000-01-02T<b>00:00</b>:00.000.550 * <p>551 * Time difference is only 1min but day fields differ.552 * <p>553 * Code example :554 * <pre><code class='java'> // successful assertions555 * LocalDateTime localDateTime1 = LocalDateTime.of(2000, 1, 1, 23, 59, 59, 999);556 * LocalDateTime localDateTime2 = LocalDateTime.of(2000, 1, 1, 00, 00, 00, 000);557 * assertThat(localDateTime1).isEqualToIgnoringHours(localDateTime2);558 *559 * // failing assertions (even if time difference is only 1ms)560 * LocalDateTime localDateTimeA = LocalDateTime.of(2000, 1, 2, 00, 00, 00, 000);561 * LocalDateTime localDateTimeB = LocalDateTime.of(2000, 1, 1, 23, 59, 59, 999);562 * assertThat(localDateTimeA).isEqualToIgnoringHours(localDateTimeB);</code></pre>563 *564 * @param other the given {@link LocalDateTime}.565 * @return this assertion object.566 * @throws AssertionError if the actual {@code LocalDateTime} is {@code null}.567 * @throws IllegalArgumentException if other {@code LocalDateTime} is {@code null}.568 * @throws AssertionError if the actual {@code LocalDateTime} is are not equal with second and nanosecond fields569 * ignored.570 */571 public SELF isEqualToIgnoringHours(LocalDateTime other) {572 Objects.instance().assertNotNull(info, actual);573 assertLocalDateTimeParameterIsNotNull(other);574 if (!haveSameYearMonthAndDayOfMonth(actual, other)) {575 throw Failures.instance().failure(info, shouldBeEqualIgnoringHours(actual, other));576 }577 return myself;578 }579 /**580 * Verifies that the actual {@link LocalDateTime} is in the [start, end] period (start and end included) according to the {@link ChronoLocalDateTime#timeLineOrder()} comparator.581 * <p>582 * {@link ChronoLocalDateTime#timeLineOrder()} compares {@code LocalDateTime} in time-line order <b>ignoring the chronology</b>, this is equivalent to comparing the epoch-day and nano-of-day.583 * <p>584 * This behaviour can be overridden by {@link AbstractLocalDateTimeAssert#usingComparator(Comparator)}.585 * <p>586 * Example:587 * <pre><code class='java'> LocalDateTime localDateTime = LocalDateTime.now();588 *589 * // assertions succeed:590 * assertThat(localDateTime).isBetween(localDateTime.minusSeconds(1), localDateTime.plusSeconds(1))591 * .isBetween(localDateTime, localDateTime.plusSeconds(1))592 * .isBetween(localDateTime.minusSeconds(1), localDateTime)593 * .isBetween(localDateTime, localDateTime);594 *595 * // assertions fail:596 * assertThat(localDateTime).isBetween(localDateTime.minusSeconds(10), localDateTime.minusSeconds(1));597 * assertThat(localDateTime).isBetween(localDateTime.plusSeconds(1), localDateTime.plusSeconds(10));</code></pre>598 *599 * @param startInclusive the start value (inclusive), expected not to be null.600 * @param endInclusive the end value (inclusive), expected not to be null.601 * @return this assertion object.602 * @throws AssertionError if the actual value is {@code null}.603 * @throws NullPointerException if start value is {@code null}.604 * @throws NullPointerException if end value is {@code null}.605 * @throws AssertionError if the actual value is not in [start, end] period.606 *607 * @since 3.7.1608 */609 public SELF isBetween(LocalDateTime startInclusive, LocalDateTime endInclusive) {610 comparables.assertIsBetween(info, actual, startInclusive, endInclusive, true, true);611 return myself;612 }613 /**614 * Same assertion as {@link #isBetween(LocalDateTime, LocalDateTime)} but here you pass {@link LocalDateTime} String representations615 * which must follow <a href="http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_LOCAL_DATE_TIME">ISO LocalDateTime format</a>616 * to allow calling {@link LocalDateTime#parse(CharSequence)} method.617 * <p>618 * Example:619 * <pre><code class='java'> LocalDateTime firstOfJanuary2000 = LocalDateTime.parse("2000-01-01T00:00:00");620 *621 * // assertions succeed:622 * assertThat(firstOfJanuary2000).isBetween("1999-12-31T23:59:59", "2000-01-01T00:00:01")623 * .isBetween("2000-01-01T00:00:00", "2000-01-01T00:00:01")624 * .isBetween("1999-12-31T23:59:59", "2000-01-01T00:00:00")625 * .isBetween("2000-01-01T00:00:00", "2000-01-01T00:00:00");626 *627 * // assertion fails:628 * assertThat(firstOfJanuary2000).isBetween("1999-01-01T00:00:01", "1999-12-31T23:59:59");</code></pre>629 *630 * @param startInclusive the start value (inclusive), expected not to be null.631 * @param endInclusive the end value (inclusive), expected not to be null.632 * @return this assertion object.633 *634 * @throws AssertionError if the actual value is {@code null}.635 * @throws NullPointerException if start value is {@code null}.636 * @throws NullPointerException if end value is {@code null}.637 * @throws DateTimeParseException if any of the given String can't be converted to a {@link LocalDateTime}.638 * @throws AssertionError if the actual value is not in [start, end] period.639 *640 * @since 3.7.1641 */642 public SELF isBetween(String startInclusive, String endInclusive) {643 return isBetween(parse(startInclusive), parse(endInclusive));644 }645 /**646 * Verifies that the actual {@link LocalDateTime} is in the ]start, end[ period (start and end excluded) according to the {@link ChronoLocalDateTime#timeLineOrder()} comparator.647 * <p>648 * {@link ChronoLocalDateTime#timeLineOrder()} compares {@code LocalDateTime} in time-line order <b>ignoring the chronology</b>, this is equivalent to comparing the epoch-day and nano-of-day.649 * <p>650 * This behaviour can be overridden by {@link AbstractLocalDateTimeAssert#usingComparator(Comparator)}.651 * <p>652 * Example:653 * <pre><code class='java'> LocalDateTime localDateTime = LocalDateTime.now();654 *655 * // assertion succeeds:656 * assertThat(localDateTime).isStrictlyBetween(localDateTime.minusSeconds(1), localDateTime.plusSeconds(1));657 *658 * // assertions fail:659 * assertThat(localDateTime).isStrictlyBetween(localDateTime.minusSeconds(10), localDateTime.minusSeconds(1));660 * assertThat(localDateTime).isStrictlyBetween(localDateTime.plusSeconds(1), localDateTime.plusSeconds(10));661 * assertThat(localDateTime).isStrictlyBetween(localDateTime, localDateTime.plusSeconds(1));662 * assertThat(localDateTime).isStrictlyBetween(localDateTime.minusSeconds(1), localDateTime);</code></pre>663 *664 * @param startExclusive the start value (exclusive), expected not to be null.665 * @param endExclusive the end value (exclusive), expected not to be null.666 * @return this assertion object.667 * @throws AssertionError if the actual value is {@code null}.668 * @throws NullPointerException if start value is {@code null}.669 * @throws NullPointerException if end value is {@code null}.670 * @throws AssertionError if the actual value is not in ]start, end[ period.671 *672 * @since 3.7.1673 */674 public SELF isStrictlyBetween(LocalDateTime startExclusive, LocalDateTime endExclusive) {675 comparables.assertIsBetween(info, actual, startExclusive, endExclusive, false, false);676 return myself;677 }678 /**679 * Same assertion as {@link #isStrictlyBetween(LocalDateTime, LocalDateTime)} but here you pass {@link LocalDateTime} String representations680 * which must follow <a href="http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_LOCAL_DATE_TIME">ISO LocalDateTime format</a>681 * to allow calling {@link LocalDateTime#parse(CharSequence)} method.682 * <p>683 * Example:684 * <pre><code class='java'> LocalDateTime firstOfJanuary2000 = LocalDateTime.parse("2000-01-01T00:00:00");685 *686 * // assertion succeeds:687 * assertThat(firstOfJanuary2000).isStrictlyBetween("1999-12-31T23:59:59", "2000-01-01T00:00:01");688 *689 * // assertions fail:690 * assertThat(firstOfJanuary2000).isStrictlyBetween("1999-01-01T00:00:01", "1999-12-31T23:59:59");691 * assertThat(firstOfJanuary2000).isStrictlyBetween("2000-01-01T00:00:00", "2000-01-01T00:00:01");692 * assertThat(firstOfJanuary2000).isStrictlyBetween("1999-12-31T23:59:59", "2000-01-01T00:00:00");</code></pre>693 *694 * @param startExclusive the start value (exclusive), expected not to be null.695 * @param endExclusive the end value (exclusive), expected not to be null.696 * @return this assertion object.697 *698 * @throws AssertionError if the actual value is {@code null}.699 * @throws NullPointerException if start value is {@code null}.700 * @throws NullPointerException if end value is {@code null}.701 * @throws DateTimeParseException if any of the given String can't be converted to a {@link LocalDateTime}.702 * @throws AssertionError if the actual value is not in ]start, end[ period.703 *704 * @since 3.7.1705 */706 public SELF isStrictlyBetween(String startExclusive, String endExclusive) {707 return isStrictlyBetween(parse(startExclusive), parse(endExclusive));708 }709 /**710 * {@inheritDoc}711 */712 @Override713 protected LocalDateTime parse(String localDateTimeAsString) {714 return LocalDateTime.parse(localDateTimeAsString);715 }716 /**717 * Returns true if both datetime are in the same year, month and day of month, hour, minute and second, false718 * otherwise.719 *720 * @param actual the actual datetime. expected not be null721 * @param other the other datetime. expected not be null722 * @return true if both datetime are in the same year, month and day of month, hour, minute and second, false723 * otherwise.724 */725 private static boolean areEqualIgnoringNanos(LocalDateTime actual, LocalDateTime other) {726 return areEqualIgnoringSeconds(actual, other) && actual.getSecond() == other.getSecond();727 }728 /**...

Full Screen

Full Screen

parse

Using AI Code Generation

copy

Full Screen

1assertThat(LocalDateTime.of(2019, 1, 1, 0, 0, 0)).parse("2019-01-01T00:00:00");2assertThat(LocalDate.of(2019, 1, 1)).parse("2019-01-01");3assertThat(LocalTime.of(0, 0, 0)).parse("00:00:00");4assertThat(OffsetDateTime.of(2019, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC)).parse("2019-01-01T00:00:00Z");5assertThat(OffsetTime.of(0, 0, 0, 0, ZoneOffset.UTC)).parse("00:00:00Z");6assertThat(ZonedDateTime.of(2019, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC)).parse("2019-01-01T00:00:00Z");7assertThat(Instant.ofEpochSecond(1546300800)).parse("2019-01-01T00:00:00Z");8assertThat(Year.of(2019)).parse("2019");9assertThat(YearMonth.of(2019, 1)).parse("2019-01");10assertThat(MonthDay.of(1, 1)).parse("--01-01");11assertThat(Duration.ofSeconds(1546300800)).parse("P2019Y1M1DT0H0M0S");

Full Screen

Full Screen

parse

Using AI Code Generation

copy

Full Screen

1import static org.assertj.core.api.Assertions.assertThat;2import java.time.LocalDateTime;3import java.time.Month;4import java.time.format.DateTimeFormatter;5public class AssertJLocalDateTimeParseExample {6 public static void main(String[] args) {7 LocalDateTime localDateTime = LocalDateTime.of(2017, Month.APRIL, 3, 10, 15, 30);8 assertThat(localDateTime).parse("2017-04-03T10:15:30");9 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");10 assertThat(localDateTime).parse("2017-04-03 10:15:30", formatter);11 }12}13assertThat(localDateTime).parse("2017-04-03T10:15:30");14assertThat(localDateTime).parse("2017-04-03 10:15:30", formatter);

Full Screen

Full Screen

parse

Using AI Code Generation

copy

Full Screen

1import java.time.LocalDateTime;2import org.assertj.core.api.Assertions;3public class ParseLocalDateTimeTest {4 public static void main(String[] args) {5 LocalDateTime dateTime = Assertions.parse("2007-12-03T10:15:30");6 System.out.println(dateTime);7 }8}9Java 8 LocalDateTime plusDays() Method Example10Java 8 LocalDateTime plusHours() Method Example11Java 8 LocalDateTime minusDays() Method Example12Java 8 LocalDateTime minusHours() Method Example13Java 8 LocalDateTime withHour() Method Example14Java 8 LocalDateTime withMinute() Method Example15Java 8 LocalDateTime withSecond() Method Example16Java 8 LocalDateTime withNano() Method Example17Java 8 LocalDateTime withYear() Method Example18Java 8 LocalDateTime withMonth() Method Example19Java 8 LocalDateTime withDayOfMonth() Method Example20Java 8 LocalDateTime withDayOfYear() Method Example21Java 8 LocalDateTime withDayOfWeek() Method Example22Java 8 LocalDateTime with() Method Example23Java 8 LocalDateTime of() Method Example24Java 8 LocalDateTime parse() Method Example25Java 8 LocalDateTime format() Method Example26Java 8 LocalDateTime now() Method Example27Java 8 LocalDateTime atZone() Method Example28Java 8 LocalDateTime atOffset() Method Example29Java 8 LocalDateTime atTime() Method Example30Java 8 LocalDateTime atDate() Method Example31Java 8 LocalDateTime toLocalDate() Method Example32Java 8 LocalDateTime toLocalTime() Method Example33Java 8 LocalDateTime toInstant() Method Example34Java 8 LocalDateTime toEpochSecond() Method Example35Java 8 LocalDateTime toEpochMilli() Method Example36Java 8 LocalDateTime isAfter() Method Example37Java 8 LocalDateTime isBefore() Method Example38Java 8 LocalDateTime isEqual() Method Example

Full Screen

Full Screen

parse

Using AI Code Generation

copy

Full Screen

1LocalDateTime localDateTime = LocalDateTime.parse("2018-10-01T12:00:00");2LocalDateTime localDateTime1 = LocalDateTime.parse("2018-10-01T12:00:00", DateTimeFormatter.ISO_DATE_TIME);3LocalDateTime localDateTime2 = LocalDateTime.parse("2018-10-01T12:00:00", DateTimeFormatter.ISO_LOCAL_DATE_TIME);4LocalDateTime localDateTime3 = LocalDateTime.parse("2018-10-01T12:00:00", DateTimeFormatter.ISO_OFFSET_DATE_TIME);5LocalDateTime localDateTime4 = LocalDateTime.parse("2018-10-01T12:00:00", DateTimeFormatter.ISO_ZONED_DATE_TIME);6LocalDateTime localDateTime5 = LocalDateTime.parse("2018-10-01T12:00:00", DateTimeFormatter.ISO_ORDINAL_DATE);7LocalDateTime localDateTime6 = LocalDateTime.parse("2018-10-01T12:00:00", DateTimeFormatter.ISO_WEEK_DATE);8LocalDateTime localDateTime7 = LocalDateTime.parse("2018-10-01T12:00:00", DateTimeFormatter.ISO_INSTANT);9LocalDateTime localDateTime8 = LocalDateTime.parse("2018-10-01T12:00:00", DateTimeFormatter.ISO_LOCAL_DATE);10LocalDateTime localDateTime9 = LocalDateTime.parse("2018-10-01T12:00:00", DateTimeFormatter.ISO_OFFSET_TIME);11LocalDateTime localDateTime10 = LocalDateTime.parse("2018-10-01T12:00:00", DateTimeFormatter.ISO_TIME);12LocalDateTime localDateTime11 = LocalDateTime.parse("2018-10-01T12:00:00", DateTimeFormatter.ISO_DATE);13LocalDateTime localDateTime12 = LocalDateTime.parse("2018-10-01T12:00:00", DateTimeFormatter.ISO_LOCAL_TIME);14LocalDateTime localDateTime13 = LocalDateTime.parse("2018-10-01T12:00:00", DateTimeFormatter.ISO_OFFSET_DATE);15LocalDateTime localDateTime14 = LocalDateTime.parse("2018-10-01T12:00:00", DateTimeFormatter.ISO_ZONED_DATE);16LocalDateTime localDateTime15 = LocalDateTime.parse("2018-10-01T12:00:00", DateTimeFormatter.ISO_DATE_TIME);17LocalDateTime localDateTime16 = LocalDateTime.parse("2018-10-01T12:00:00", DateTimeFormatter.ISO_DATE

Full Screen

Full Screen

parse

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.api.Assertions;2import org.assertj.core.api.AbstractLocalDateTimeAssert;3import java.time.LocalDateTime;4class AssertJExample {5 public static void main(String[] args) {6 LocalDateTime expected = LocalDateTime.of(2019, 10, 3, 10, 15, 30);7 String date = "2019-10-03T10:15:30";8 AbstractLocalDateTimeAssert<?> assertion = Assertions.assertThat(expected);9 assertion = assertion.isEqualTo(expected);10 assertion = assertion.isEqualTo(date);11 }12}13 at org.assertj.core.api.AbstractLocalDateTimeAssert.isEqualTo(AbstractLocalDateTimeAssert.java:87)14 at org.assertj.core.api.AbstractLocalDateTimeAssert.isEqualTo(AbstractLocalDateTimeAssert.java:37)15 at AssertJExample.main(AssertJExample.java:16)16The isAfter() and isBefore() methods17import org.assertj.core.api.Assertions;18import org.assertj.core.api.AbstractLocalDateTimeAssert;19import java.time.LocalDateTime;20class AssertJExample {21 public static void main(String[] args) {22 LocalDateTime expected = LocalDateTime.of(2019, 10, 3, 10, 15, 30);23 LocalDateTime date = LocalDateTime.of(2019, 10, 3, 10, 15, 30);24 LocalDateTime dateBefore = LocalDateTime.of(2019, 10, 3, 10, 15, 29);25 LocalDateTime dateAfter = LocalDateTime.of(2019, 10, 3, 10, 15, 31);26 AbstractLocalDateTimeAssert<?> assertion = Assertions.assertThat(expected);27 assertion = assertion.isAfter(dateBefore);

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful