William R
William R

Reputation: 739

How to calculate 'n' days interval date in functional style?

I am trying to calculae the interval of 'n' days from start date to end date.Function signature would have start_date,end_date, interval as argument which return a map with list of start, end days of given intervals.

Example: start_date:2018-01-01 , End_date : 2018-02-20 interval: 20

Expected Output:

2018-01-01 , 2018-01-20 (20 days)

2018-01-21 , 2018-02-09 (20 days)

2018-02-09 , 2018-01-20 (remaining)

I tried to write in scala but i dont feel it's a proper functional style of writing.

case class DateContainer(period: String, range: (LocalDate, LocalDate))

    def generateDates(startDate: String, endDate: String,interval:Int): Unit = {

      import java.time._
      var lstDDateContainer = List[DateContainer]()
      var start = LocalDate.parse(startDate)
      val end = LocalDate.parse(endDate)
      import java.time.temporal._
      var futureMonth = ChronoUnit.DAYS.addTo(start, interval)
      var i = 1
      while (end.isAfter(futureMonth)) {
        lstDDateContainer = DateContainer("P" + i, (start, futureMonth)):: lstDDateContainer
        start=futureMonth
        futureMonth = ChronoUnit.DAYS.addTo(futureMonth, interval)
        i += 1
      }
      lstDDateContainer= DateContainer("P" + i, (start, end))::lstDDateContainer
      lstDDateContainer.foreach(println)

    }

    generateDates("2018-01-01", "2018-02-20",20)

Could anyone help me to write in a functional style.

Upvotes: 0

Views: 436

Answers (4)

jrook
jrook

Reputation: 3519

I offer a solution that produces a slightly different result than given in the question but could be easily modified to get the desired answer:

//Preliminaries
val fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd")
val startDate ="2018-01-01"
val endDate = "2018-02-21"
val interval = 20L   
val d1 = LocalDate.parse(startDate, fmt)
val d2 = LocalDate.parse(endDate, fmt)

//The main code
Stream.continually(interval)
  .scanLeft((d1, d1.minusDays(1), interval)) ((x,y) => {
    val finDate = x._2.plusDays(y)
    if(finDate.isAfter(d2))
      (x._2.plusDays(1), d2, ChronoUnit.DAYS.between(x._2, d2))
    else
      (x._2.plusDays(1), x._2.plusDays(y), y)
  }).takeWhile(d => d._3 > 0).drop(1).toList

Result:

(2018-01-01,2018-01-20,20)
(2018-01-21,2018-02-09,20)
(2018-02-10,2018-02-21,12)

The idea is to scan a 3-tuple through a stream of interval and stop when no more days are remaining.

Upvotes: 1

Terry Dactyl
Terry Dactyl

Reputation: 1868

Something like (Untested):

def dates(startDate: LocalDate, endDate: LocalDate, dayInterval: Int): List[(LocalDate, LocalDate, Int)] = {

  if(startDate.isAfter(endDate)) {
    Nil
  }
  else {

    val nextStart = startDate.plusDays(dayInterval)

    if(nextStart.isAfter(startDate)) {
      List((startDate, endDate, ChronoUnit.DAYS.between(startDate, endDate)))
    }
    else {
      (startDate, nextStart, dayInterval) :: dates(nextStart, endDate, dayInterval)
    }
  }
}

Upvotes: 1

jwvh
jwvh

Reputation: 51271

Use the java.time library to generate the dates and Stream.iterate() to generate the sequence of intervals.

import java.time.LocalDate

def generateDates( startDate   :LocalDate
                 , endDate     :LocalDate
                 , dayInterval :Int ) :Unit = {
  val intervals =
    Stream.iterate((startDate, startDate plusDays dayInterval-1)){
      case (_,lastDate) => 
        val nextDate = lastDate plusDays dayInterval
        (lastDate plusDays 1, if (nextDate isAfter endDate) endDate 
                              else nextDate)
    }.takeWhile(_._1 isBefore endDate)
  println(intervals.mkString("\n"))
}

usage:

generateDates(LocalDate.parse("2018-01-01"), LocalDate.parse("2018-02-20"), 20)

// (2018-01-01,2018-01-20)
// (2018-01-21,2018-02-09)
// (2018-02-10,2018-02-20)

Upvotes: 1

y2k-shubham
y2k-shubham

Reputation: 11607

If your'e open to using Joda for date-time manipulations, here's what I use

import org.joda.time.{DateTime, Days}

// given from & to dates, find no of days elapsed in between (integer)
def getDaysInBetween(from: DateTime, to: DateTime): Int = Days.daysBetween(from, to).getDays

def getDateSegments(from: DateTime, to: DateTime, interval: Int): Seq[(DateTime, DateTime)] = {
    // no of days between from & to dates
    val days: Int = DateTimeUtils.getDaysInBetween(from, to) + 1

    // no of segments (date ranges) between to & from dates
    val segments: Int = days / interval

    // (remaining) no of days in last range
    val remainder: Int = days % interval

    // last date-range
    val remainderRanges: Seq[(DateTime, DateTime)] =
      if (remainder != 0) from -> from.plusDays(remainder - 1) :: Nil
      else Nil

    // all (remaining) date-ranges + last date-range
    (0 until segments).map { segment: Int =>
      to.minusDays(segment * interval + interval - 1) -> to.minusDays(segment * interval)
    } ++ remainderRanges
  }

Upvotes: 0

Related Questions