如何在 Scala 中获取两个日期之间的工作日列表(不包括假期和周末)?
问问题
3453 次
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 回答