我有以下代码来获取提供的日期范围期间的开始日期
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
我希望通过将星期的第一天作为星期一(或者第一天作为第1天或者给出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)
答案 0 :(得分:0)
我做了一些试验并找到了以下解决方案,我将最终结果日期转换为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,因为控制台为Joda DateTime的隐式排序提供了以下错误
error: No implicit Ordering defined for org.joda.time.DateTime.
var dateSlots = scala.collection.SortedMap[DateTime, DateTime]()