Help in Populating a array of date tuples

Hi Friends

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

val predicates =
“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 =
“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

Keeping the date as a string doesn’t make it easy to manipulate as date. Use e.g. LocalDate (Java Platform SE 8 ) instead

The final solution would then be something like:

def predicateRange(startDate: LocalDate, endDate: LocalDate): Seq[(LocalDate, LocalDate)]= {
    // make generator of never ending sequence of periods, but then ...
    .iterate(startDate -> startDate.plusDays(1)) {
      case (periodStart, periodEnd) =>
        periodStart.plusDays(2) -> periodEnd.plusDays(2)
    // ... take just a part of that sequence
    .takeWhile { case (_, periodEnd) => !periodEnd.isAfter(endDate) }
    .toSeq // you could do .toArray instead, but arrays are mutable and
           // therefore not recommended unless you need mutability
1 Like

Thanks tarsa,it works