2

Scalaの2つの日付の間の営業日(休日と週末を除く)のリストを取得するにはどうすればよいですか?

4

1 に答える 1

1

この質問で推奨されているように、時間を操作するための基礎として Joda Time を使用しました。コードを取得して、Scala の愛をそれに適用しました。

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
    }}
  }
}
于 2012-10-16T15:49:51.017 に答える