给定一个范围,在 Scala 中获取该范围内的所有日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19426807/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Given a range, getting all dates within that range in Scala
提问by Core_Dumped
I need to make a function in scala that, given a range of dates, gives me a list of the range. I am relatively new in Scala and I am not able to figure out how to write the right 'for' loop for the same. This is what I have done so far:
我需要在 scala 中创建一个函数,给定一个日期范围,给我一个范围列表。我在 Scala 中相对较新,我无法弄清楚如何为此编写正确的“for”循环。这是我到目前为止所做的:
def calculateDates(from: LocalDate, until: LocalDate): Seq[LocalDate] = {
var dateArray = []
//for (LocalDate date <- from; !date.isAfter(to); date <- date.plusDays(1))
for(date <- from to until)
{
dateArray :+ date
}
return dateArray
}
I do not know how to iterate over the range.
我不知道如何迭代范围。
回答by sandris
If you happen to use the Java 1.8 DateTimeAPI (or its 1.7 backport threeten), then you could write
如果您碰巧使用 Java 1.8 DateTimeAPI(或其 1.7 backport threeten),那么您可以编写
def between(fromDate: LocalDate, toDate: LocalDate) = {
fromDate.toEpochDay.until(toDate.toEpochDay).map(LocalDate.ofEpochDay)
}
回答by Ashalynd
val numberOfDays = Days.daysBetween(from, until).getDays()
for (f<- 0 to numberOfDays) yield from.plusDays(f)
回答by jester
Try this
试试这个
def dateRange(start: DateTime, end: DateTime, step: Period): Iterator[DateTime] =
Iterator.iterate(start)(_.plus(step)).takeWhile(!_.isAfter(end))
To generate every date, you can set the step to 1 day like
要生成每个日期,您可以将步骤设置为 1 天,例如
val range = dateRange(
<yourstartdate>,
<yourenddate>,
Period.days(1))
回答by Max
Use Lamma Date
使用南丫岛日期
$ scala -cp lamma_2.11-1.1.2.jar
Welcome to Scala version 2.11.0 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_25).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import io.lamma.Date
import io.lamma.Date
scala> Date(2015, 7, 7) to Date(2015, 7, 10) foreach println
Date(2015,7,7)
Date(2015,7,8)
Date(2015,7,9)
Date(2015,7,10)
This DateRangeis evaluated in a lazy way. Feel free to construct a date range of 5000 years. :)
这DateRange是以懒惰的方式评估的。随意构建一个 5000 年的日期范围。:)
回答by Xavier Guihot
When running Scalaon Java 9+, we can take advantage of the new java.time.LocalDate::datesUntil:
在运行Scala时Java 9+,我们可以利用新的java.time.LocalDate::datesUntil:
import java.time.LocalDate
import collection.JavaConverters._
// val start = LocalDate.of(2018, 9, 24)
// val end = LocalDate.of(2018, 9, 28)
start.datesUntil(end).iterator.asScala.toList
// List[LocalDate] = List(2018-09-24, 2018-09-25, 2018-09-26, 2018-09-27)
And to include the last date within the range:
并包括范围内的最后日期:
start.datesUntil(end.plusDays(1)).iterator.asScala.toList
// List[LocalDate] = List(2018-09-24, 2018-09-25, 2018-09-26, 2018-09-27, 2018-09-28)
回答by user987339
Since Scala is a functional, go with a recursion:
由于 Scala 是函数式的,所以使用递归:
def calculateDates(from: LocalDate, until: LocalDate): Seq[LocalDate] = {
if from.compareTo(until) > 1
return[]
else
return from :: calculateDates(from.plusDays(1), until)
}

