kotest-assertions-core / io.kotest.matchers.date / haveSameMonth

haveSameMonth

fun haveSameMonth(date: LocalDate): Matcher<LocalDate>

Matcher that compares months of LocalDates

Verifies that two dates have exactly the same month, ignoring any other fields. For example, 09/02/1998 has the same month as 10/02/2018, and the matcher will have a positive result for this comparison

    val firstDate = LocalDate.of(1998, 2, 9)
    val secondDate = LocalDate.of(1998, 3, 9)

    firstDate shouldNotHaveSameMonthAs secondDate    //  Assertion passes


    val firstDate = LocalDate.of(1998, 2, 9)
    val secondDate = LocalDate.of(2018, 2, 10)

    firstDate shouldNotHaveSameMonthAs  secondDate   //  Assertion fails, 2 == 2, and we expected a difference

See Also

LocalDate.shouldHaveSameMonthAs

LocalDate.shouldNotHaveSameMonthAs

fun haveSameMonth(date: LocalDateTime): Matcher<LocalDateTime>

Matcher that compares months of LocalDateTimes

Verifies that two DateTimes have exactly the same month, ignoring any other fields. For example, 09/02/1998 10:00:00 has the same month as 10/02/2018 11:30:30, and the matcher will have a positive result for this comparison

    val firstDate = LocalDateTime.of(1998, 2, 9, 10, 0, 0)
    val secondDate = LocalDateTime.of(1998, 3, 10, 11, 30, 30)

    firstDate shouldNotHaveSameMonthAs secondDate    //  Assertion passes


    val firstDate = LocalDateTime.of(2018, 2, 9, 10, 0, 0)
    val secondDate = LocalDateTime.of(1998, 2, 10, 1, 30, 30)

    firstDate shouldNotHaveSameMonthAs  secondDate   //  Assertion fails, 2 == 2, and we expected a difference

See Also

LocalDateTime.shouldHaveSameMonthAs

LocalDateTime.shouldNotHaveSameMonthAs

fun haveSameMonth(date: ZonedDateTime): Matcher<ZonedDateTime>

Matcher that compares months of ZonedDateTimes

Verifies that two ZonedDateTimes have exactly the same month, ignoring any other fields. For example, 09/02/1998 10:00:00 -03:00 America/Sao_Paulo has the same month as 10/02/2018 11:30:30 -05:00 America/Chicago, and the matcher will have a positive result for this comparison

    val firstDate = ZonedDateTime.of(1998, 2, 9, 10, 0, 0, 0, ZoneId.of("America/Sao_Paulo"))
    val secondDate = ZonedDateTime.of(1998, 3, 9, 19, 0, 0, 0, ZoneId.of("America/Sao_Paulo"))

    firstDate shouldNotHaveSameMonthAs secondDate    //  Assertion passes


    val firstDate = ZonedDateTime.of(1998, 2, 9, 10, 0, 0, 0, ZoneId.of("America/Sao_Paulo"))
    val secondDate = ZonedDateTime.of(2018, 2, 10, 1, 30, 30, 30, ZoneId.of("America/Chicago"))

    firstDate shouldNotHaveSameMonthAs  secondDate   //  Assertion fails, 2 == 2, and we expected a difference

See Also

ZonedDateTime.shouldHaveSameMonthAs

ZonedDateTime.shouldNotHaveSameMonthAs

fun haveSameMonth(date: OffsetDateTime): Matcher<OffsetDateTime>

Matcher that compares months of OffsetDateTimes

Verifies that two ZonedDateTimes have exactly the same month, ignoring any other fields. For example, 09/02/1998 10:00:00 -03:00 has the same month as 10/02/1998 11:30:30 -05:00, and the matcher will have a positive result for this comparison

    val firstDate = OffsetDateTime.of(1998, 2, 9, 10, 0, 0, 0, ZoneOffset.ofHours(-3))
    val secondDate = OffsetDateTime.of(2018, 2, 10, 11, 30, 30, 30, ZoneOffset.ofHours(-5))

    firstDate should haveSameMonth(secondDate)   //  Assertion passes


    val firstDate = OffsetDateTime.of(1998, 2, 9, 10, 0, 0, 0, ZoneOffset.ofHours(-3))
    val secondDate = OffsetDateTime.of(1998, 3, 9, 10, 0, 0, 0, ZoneOffset.ofHours(-3))

    firstDate shouldNot haveSameMonth(secondDate)    //  Assertion passes

See Also

OffsetDateTime.shouldHaveSameMonthAs

OffsetDateTime.shouldNotHaveSameMonthAs