英文:
Parse list of dates to list string in scala
问题
import org.joda.time.LocalDate
import org.joda.time._
def getDates(startDate: String, endDate: String): Any = {
val from = LocalDate.parse(startDate)
val until = LocalDate.parse(endDate)
val numberOfDays = Days.daysBetween(from, until).getDays()
for (f <- 0 to numberOfDays) yield from.plusDays(f)
}
println(getDates("20200101", "20200131"))
// This returns -> Vector(2020-01-01, 2020-01-31)
// Need to convert returned Vector to List[String] in which elements are of format "yyyyMMdd"
val dateList = getDates("20200101", "20200131").map(date => date.toString("yyyyMMdd")).toList
// Expected Output: List(20200101, ...., 20200131)
英文:
Need to format date according to yyyyMMdd
below code generates a list of dates
import org.joda.time.LocalDate
import org.joda.time._
def getDates(startDate: String,endDate: String): Any = {
val from = LocaDate.parse(startDate)
val until = LocalDate.parse(endDate)
val numberOfDays = Days.daysBetween(from, until).getDays()
for (f<- 0 to numberOfDays) yield from.plusDays(f)
}
println(getDates("20200101","20200131"))
This returns -> Vector(2020-01-01,2020-01-31)
Need to convert returned Vector to List[String] in which elements are of format "yyyyMMdd"
Expected Output: List(20200101,....,20200131)
答案1
得分: 2
如果getDates()
的输入是2个字符串,并且期望的输出是List[String]
,那么我真的看不出使用旧的过时的Joda Time库的任何理由。java.time
库更新且功能更丰富。
它还提供了datesUntil()
方法,基本上可以实现您想要的功能,只是它返回一个java.util.stream.Stream
,从Java Stream
转换为Scala List
会有点麻烦,这取决于您正在使用的Scala版本。
以下是一个示例,在Scala 2.13.x中如何实现:
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import scala.jdk.StreamConverters.StreamHasToScala
def getDates(startDate: String, endDate: String): List[String] = {
val pattern = DateTimeFormatter.ofPattern("yyyyMMdd")
LocalDate.parse(startDate, pattern)
.datesUntil(LocalDate.parse(endDate, pattern).plusDays(1))
.toScala(List)
.map(_.format(pattern))
}
您会注意到,此示例没有对有效输入格式进行检查,因此这仅用于演示,不适用于生产环境的代码。
英文:
If the getDates()
input is 2 strings, and the desired output is List[String]
, then I don't really see any reason to use the old and outdated Joda Time library. The java.time
library is more recent and feature-full.
It also offers the datesUntil()
method, which does pretty much what you want, except that it returns a java.util.stream.Stream
, which is a bit of a pain because the transition from Java Stream
to Scala List
will depend on the Scala version you're running.
Here, for example, is how you might do it in Scala 2.13.x:
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import scala.jdk.StreamConverters.StreamHasToScala
def getDates(startDate:String, endDate:String):List[String] = {
val pattern = DateTimeFormatter.ofPattern("yyyyMMdd")
LocalDate.parse(startDate, pattern)
.datesUntil(LocalDate.parse(endDate, pattern).plusDays(1))
.toScala(List)
.map(_.format(pattern))
}
You'll notice that there is no checking for valid input format, so this is for demonstration and not production-worthy code.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论