解析日期列表为字符串列表在Scala中的实现方式:

huangapple go评论73阅读模式
英文:

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&lt;- 0 to numberOfDays) yield from.plusDays(f)
}

println(getDates(&quot;20200101&quot;,&quot;20200131&quot;))

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(&quot;yyyyMMdd&quot;)
  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.

huangapple
  • 本文由 发表于 2020年9月15日 10:10:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63894072.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定