如何在scala中的两个日期之间获得工作日

时间:2012-10-16 15:49:51

标签: scala datetime jodatime

如何获取Scala中两个日期之间的工作日列表(不包括假期和周末)?

2 个答案:

答案 0 :(得分:2)

我使用Joda Time作为按照推荐in this question的时间工作的基础,我只是接受了代码并对它应用了一些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
    }}
  }
}

答案 1 :(得分:0)

通知toDateMidnight在DST开始的那天抛出异常。