如何根据Scala提供的范围以及一周中的第一天开始开始和结束日期



我有以下代码用于开始日期的开始日期

def dayIterator(start: DateTime, end: DateTime, period: String) = {
    period match {
      case "day"   => Iterator.iterate(start)(_ plusDays 1) takeWhile (_ isBefore end)
      case "week"  => Iterator.iterate(start)(_ plusWeeks 1) takeWhile (_ isBefore end)
      case "month" => Iterator.iterate(start)(_ plusMonths 1) takeWhile (_ isBefore end)
    }
  }

以下代码根据周期提取日期

 scala> dayIterator(new DateTime("2017-09-13"), new DateTime("2017-09-27"), "week").foreach(println)
    2017-09-13T00:00:00.000+05:30
    2017-09-20T00:00:00.000+05:30

我想通过将一周的第一天(或几个月的第一天为第一个或start_date)来抵消一周(或一个月)的开始日期和结束日期因此,我希望输出就像关注(提到参考的日名)

2017-09-13T00:00:00.000+05:30 (Wednesday) -- 2017-09-17T00:00:00.000+05:30 (Sunday)
2017-09-18T00:00:00.000+05:30 (Monday) -- 2017-09-24T00:00:00.000+05:30 (Sunday)
2017-09-25T00:00:00.000+05:30 (Monday) -- 2017-09-27T00:00:00.000+05:30 (Wednesday)

我已经进行了一些试验,并找到了解决方案,我将最终结果日期转换为java.util.date,并将其放入SortedMap

以下是dayGroupgenerator函数,它负责抵消

def dayGroupGenerator(startx: DateTime, endx: DateTime, period: String): scala.collection.mutable.ListBuffer[(Date, Date)] = {
    var start = startx
    var end = endx
    var dateSlots = scala.collection.mutable.ListBuffer[(Date, Date)]()
    if (start.isAfter(end)) {
      dateSlots
    }
    else if (start.equals(end)) {
      dateSlots += ((start.toDate, end.toDate))
    }
    else {
      period match {
        case "day" => {
          while (start.equals(end) || start.isBefore(end)) {
            dateSlots += ((start.toDate, (if (start.equals(end) || start.isAfter(end)) end else start).toDate))
            start = start.plusDays(1)
          }
        }
        case "week" => {
          if (start.dayOfWeek.get != 1) {
            dateSlots += ((start.toDate, (if (start.withDayOfWeek(7).isAfter(end)) end else start.withDayOfWeek(7)).toDate))
            start = start.withDayOfWeek(7).plusDays(1)
          }
          while (start.equals(end) || start.isBefore(end)) {
            dateSlots += ((start.toDate, (if (start.equals(end) || start.withDayOfWeek(7).isAfter(end)) end else start.withDayOfWeek(7)).toDate))
            start = start.withDayOfWeek(7).plusDays(1)
          }
        }
        case "month" => {
          if (start.dayOfMonth.get != 1) {
            dateSlots += ((start.toDate, (if (start.dayOfMonth.withMaximumValue.isAfter(end)) end else start.dayOfMonth.withMaximumValue).toDate))
            start = start.dayOfMonth.withMaximumValue.plusDays(1)
          }
          while (start.equals(end) || start.isBefore(end)) {
            dateSlots += ((start.toDate, (if (start.equals(end) || start.dayOfMonth.withMaximumValue.isAfter(end)) end else start.dayOfMonth.withMaximumValue).toDate))
            start = start.dayOfMonth.withMaximumValue.plusDays(1)
          }
        }
      }
    }
    dateSlots
  }

您可以在Scala控制台上粘贴上述代码。通过使用以下Scala控制台示例来查看以上函数,以查看我尝试实现的目标

scala> import scala.collection.SortedMap
scala> import java.util.Date
scala> import org.joda.time.Days
scala> import org.joda.time.DateTime

scala> dayGroupGenerator(new DateTime("2017-01-07"), new DateTime("2017-01-07"), "day").foreach(println)
scala> dayGroupGenerator(new DateTime("2017-01-31"), new DateTime("2017-02-01"), "week").foreach(println)
scala> dayGroupGenerator(new DateTime("2017-01-11"), new DateTime("2017-03-01"), "month").foreach(println)
scala> dayGroupGenerator(new DateTime("2017-02-01"), new DateTime("2017-02-03"), "day").foreach(println)
scala> dayGroupGenerator(new DateTime("2017-02-11"), new DateTime("2017-02-27"), "week").foreach(println)
scala> dayGroupGenerator(new DateTime("2017-03-13"), new DateTime("2017-05-30"), "month").foreach(println)

注意:我尚未在返回类型(MAP [])中使用DateTime,并且使用了更简单的Java.util.date,因为控制台给我带来了以下错误。

error: No implicit Ordering defined for org.joda.time.DateTime.
           var dateSlots = scala.collection.SortedMap[DateTime, DateTime]()

最新更新