How can I get a list of business days (exclude the holidays and weekends) between two dates in Scala?
Asked
Active
Viewed 3,868 times
1 Answers
1
I used Joda Time as the basis for working with times as recommended in this question, I just took the code and applied some Scala love to it!
import scala.collection.JavaConversions.asScalaIterator
import org.joda.time.DateTime
import org.joda.time.Days
import org.joda.time.DurationFieldType
import org.joda.time.DateTimeConstants
object DateHelpers {
// populate with your own holidays, this is a small subset of canadian holidays
private val holidays = List(
new DateTime("2012-01-02").toDateMidnight(),
new DateTime("2012-05-21").toDateMidnight(),
new DateTime("2012-07-01").toDateMidnight(),
new DateTime("2012-08-06").toDateMidnight(),
new DateTime("2012-09-03").toDateMidnight(),
new DateTime("2012-10-08").toDateMidnight(),
new DateTime("2012-11-12").toDateMidnight(),
new DateTime("2012-12-25").toDateMidnight(),
new DateTime("2012-12-26").toDateMidnight()
)
def businessDaysBetween(startDate: DateTime, endDate: DateTime): Seq[DateTime] = {
val daysBetween = Days.daysBetween(startDate.toDateMidnight(), endDate.toDateMidnight()).getDays()
1 to daysBetween map { startDate.withFieldAdded(DurationFieldType.days(), _)} diff holidays filter { _.getDayOfWeek() match {
case DateTimeConstants.SUNDAY | DateTimeConstants.SATURDAY => false
case _ => true
}}
}
}

Community
- 1
- 1

Jeffrey Cameron
- 9,975
- 10
- 45
- 77
-
You could check out ScalaTime https://github.com/jorgeortiz85/scala-time it's a wrapper for JodaTime. – Emil L Oct 16 '12 at 16:59