Populating a array of date tuples

Viewed 79

i am trying to pass a list of date ranges needs to be in the below format.

val predicates =
Array(
“2021-05-16” → “2021-05-17”,
“2021-05-18” → “2021-05-19”,
“2021-05-20” → “2021-05-21”)

I am then using map to create a range of conditions that will be passed to the jdbc method.

val predicates =
  Array(
    “2021-05-16” → “2021-05-17”,
    “2021-05-18” → “2021-05-19”,
    “2021-05-20” → “2021-05-21”
  ).map { case (start, end) =>
    s"cast(NEW_DT as date) >= date ‘$start’ AND cast(NEW_DT as date) <= date ‘$end’"
  }

The process will need to run daily and i need to dynamically populate these values as i cannot use the hard coded way.

Need help in how i can return these values from a method with incrementing start_date and end_date tuples that can generate like above.I had a mere idea like below but as i am new to scala not able to figure out. Please help

def predicateRange(start_date: String, end_date: String): Array[(String,String)] = {
  // iterate over the date values and add + 1 to both start and end and return the tuple
}
2 Answers

This assumes that every range is the same duration, and that each date range starts the next day after the end of the previous range.

import java.time.LocalDate
import java.time.format.DateTimeFormatter

def dateRanges(start: String
              ,rangeLen: Int
              ,ranges: Int): Array[(String,String)] = {
  val startDate =
    LocalDate.parse(start, DateTimeFormatter.ofPattern("yyyy-MM-dd"))
  Array.iterate(startDate -> startDate.plusDays(rangeLen), ranges){
    case (_, end) => end.plusDays(1) -> end.plusDays(rangeLen+1)
  }.map{case (s,e) => (s.toString, e.toString)}
}

usage:

dateRanges("2021-05-16", 1, 3)
//res0: Array[(String, String)] = Array((2021-05-16,2021-05-17), (2021-05-18,2021-05-19), (2021-05-20,2021-05-21))

You can use following method to generate your tuple array,

import java.time.LocalDate
import java.time.format.DateTimeFormatter

def generateArray3(startDateString: String, endDateString: String): Array[(String, String)] = {
  val dateFormatter = DateTimeFormatter.ISO_LOCAL_DATE
  val startDate = LocalDate.parse(startDateString)
  val endDate = LocalDate.parse(endDateString)

  val daysCount = startDate.until(endDate).getDays

  val dateStringTuples = Array.tabulate(daysCount)(i => {
    val firstDate = startDate.plusDays(i)
    val secondDate = startDate.plusDays(i + 1)
    (dateFormatter.format(firstDate), dateFormatter.format(secondDate))
  })

  dateStringTuples
}

Usage:

println("--------------------------")
generateArray("2021-02-27", "2021-03-02").foreach(println)
println("--------------------------")
generateArray("2021-05-27", "2021-06-02").foreach(println)
println("--------------------------")
generateArray("2021-12-27", "2022-01-02").foreach(println)
println("--------------------------")

output :

--------------------------
(2021-02-27,2021-02-28)
(2021-02-28,2021-03-01)
(2021-03-01,2021-03-02)
--------------------------
(2021-05-27,2021-05-28)
(2021-05-28,2021-05-29)
(2021-05-29,2021-05-30)
(2021-05-30,2021-05-31)
(2021-05-31,2021-06-01)
(2021-06-01,2021-06-02)
--------------------------
(2021-12-27,2021-12-28)
(2021-12-28,2021-12-29)
(2021-12-29,2021-12-30)
(2021-12-30,2021-12-31)
(2021-12-31,2022-01-01)
(2022-01-01,2022-01-02)
--------------------------
Related