Period duplication check (LocalTime, LocalDate, LocalDateTime)

I feel like I'm thinking about logic every time, so make a note of it.

--There is often a pattern that the end of the period is null (that is, the target period from the start to the future), so it is also supported. --Even when the end of period 1 and the start of period 2 are exactly the same, such as "10:00 to 11:00" and "11:00 to 12:00", it is judged that there is duplication. To make this pattern unique, set ! Start2.isAfter (end1) to start2.isBefore (end1) and ! End2.isBefore (start1) to ʻend2.isAfter (start1)`. To do.

 /**
   *Check for duplicate periods(Local Time version)
   *Overlapping periods at a single point are also considered overlapping.
   * e.g.)
   *Period 1=> 10:00 ~ 11:00
   *Period 2=> 11:00 ~ 12:00
   *result=>There is duplication(true)
   *
   * @param start1 Start of period 1
   * @param end1 End of period 1
   * @param start2 Start of period 2
   * @param end2 End of period 2
   * @return true if duplicated
   */
  def isOverlapped(start1: LocalTime, end1: LocalTime, start2: LocalTime, end2: LocalTime): Boolean =
    !start2.isAfter(end1) && !end2.isBefore(start1)

  /**
   *Check for duplicate periods(Local Time version,End of period Option possible)
   *Overlapping periods at a single point are also considered overlapping.
   * e.g.)
   *Period 1=> 10:00 ~ 11:00
   *Period 2=> 11:00 ~ 12:00
   *result=>There is duplication(true)
   *
   * @param start1 Start of period 1
   * @param end1Opt End of period 1(If None, the target period from the start)
   * @param start2 Start of period 2
   * @param end2Opt End of period 2(If None, the target period from the start)
   * @return true if duplicated
   */
  def isOverlapped(start1: LocalTime, end1Opt: Option[LocalTime], start2: LocalTime, end2Opt: Option[LocalTime]): Boolean =
    end1Opt.fold(true)(!start2.isAfter(_)) && end2Opt.fold(true)(!_.isBefore(start1))

  /**
   *Check for duplicate periods(Local Date version)
   *Overlapping periods at a single point are also considered overlapping.
   * e.g.)
   *Period 1=> 2020-10-01 ~ 2020-10-10
   *Period 2=> 2020-10-10 ~ 2020-10-20
   *result=>There is duplication(true)
   *
   * @param start1 Start of period 1
   * @param end1 End of period 1
   * @param start2 Start of period 2
   * @param end2 End of period 2
   * @return true if duplicated
   */
  def isOverlapped(start1: LocalDate, end1: LocalDate, start2: LocalDate, end2: LocalDate): Boolean =
    !start2.isAfter(end1) && !end2.isBefore(start1)

  /**
   *Check for duplicate periods(Local Date version,End of period Option possible)
   *Overlapping periods at a single point are also considered overlapping.
   * e.g.)
   *Period 1=> 2020-10-01 ~ 2020-10-10
   *Period 2=> 2020-10-10 ~ 2020-10-20
   *result=>There is duplication(true)
   *
   * @param start1 Start of period 1
   * @param end1Opt End of period 1(If None, the target period from the start)
   * @param start2 Start of period 2
   * @param end2Opt End of period 2(If None, the target period from the start)
   * @return true if duplicated
   */
  def isOverlapped(start1: LocalDate, end1Opt: Option[LocalDate], start2: LocalDate, end2Opt: Option[LocalDate]): Boolean =
    end1Opt.fold(true)(!start2.isAfter(_)) && end2Opt.fold(true)(!_.isBefore(start1))

  /**
   *Check for duplicate periods(LocalDateTime version)
   *Overlapping periods at a single point are also considered overlapping.
   * e.g.)
   *Period 1=> 2020-10-01 10:00:00 ~ 2020-10-10 20:00:00
   *Period 2=> 2020-10-10 20:00:00 ~ 2020-10-20 20:00:00
   *result=>There is duplication(true)
   *
   * @param start1 Start of period 1
   * @param end1 End of period 1
   * @param start2 Start of period 2
   * @param end2 End of period 2
   * @return true if duplicated
   */
  def isOverlapped(start1: LocalDateTime, end1: LocalDateTime, start2: LocalDateTime, end2: LocalDateTime): Boolean =
    !start2.isAfter(end1) && !end2.isBefore(start1)

  /**
   *Check for duplicate periods(LocalDateTime version,End of period Option possible)
   *Overlapping periods at a single point are also considered overlapping.
   * e.g.)
   *Period 1=> 2020-10-01 10:00:00 ~ 2020-10-10 20:00:00
   *Period 2=> 2020-10-10 20:00:00 ~ 2020-10-20 20:00:00
   *result=>There is duplication(true)
   *
   * @param start1 Start of period 1
   * @param end1Opt End of period 1(If None, the target period from the start)
   * @param start2 Start of period 2
   * @param end2Opt End of period 2(If None, the target period from the start)
   * @return true if duplicated
   */
  def isOverlapped(start1: LocalDateTime, end1Opt: Option[LocalDateTime], start2: LocalDateTime, end2Opt: Option[LocalDateTime]): Boolean =
    end1Opt.fold(true)(!start2.isAfter(_)) && end2Opt.fold(true)(!_.isBefore(start1))

Recommended Posts

Period duplication check (LocalTime, LocalDate, LocalDateTime)
[Java] Date period duplication check sample