【问题标题】:Find dates between two dates scala查找两个日期之间的日期
【发布时间】:2020-07-19 10:17:49
【问题描述】:

我正在尝试提取两个日期之间的日期。

如果我的输入是: 开始日期:2020_04_02 结束日期:2020_06_02

输出应该是 - List("2020_04_02","2020_04_03", "2020_04_04", "2020_04_05", "2020_04_06")

到目前为止我已经尝试过:

   val beginDate = LocalDate.parse(startDate, formatter)
   val lastDate = LocalDate.parse(endDate, formatter)
   beginDate.datesUntil(lastDate.plusDays(1)).iterator().asScala.map(date => formatter.format(date)).toList


 import java.time.format.DateTimeFormatter
 private def formatter = DateTimeFormatter.ofPattern("yyyy_MM_dd")

但我认为它甚至可以以更精致的方式完成

【问题讨论】:

    标签: scala scala-collections


    【解决方案1】:

    您所做的是正确的,但您的结束日期与您的预期不符:

    
        import java.time.format.DateTimeFormatter
        import java.time.LocalDate
        import scala.jdk.CollectionConverters._
    
        private def formatter = DateTimeFormatter.ofPattern("yyyy_MM_dd")
        val startDate = "2020_04_02"
        val endDate1 = "2020_04_06" // "2020_06_02"
        val endDate2 = "2020_06_02"
        val beginDate = LocalDate.parse(startDate, formatter)
        val lastDate1 = LocalDate.parse(endDate1, formatter)
        val lastDate2 = LocalDate.parse(endDate2, formatter)
        val res1 =  beginDate.datesUntil(lastDate1.plusDays(1)).iterator().asScala.map(date => formatter.format(date)).toList
        val res2 =  beginDate.datesUntil(lastDate2.plusDays(1)).iterator().asScala.map(date => formatter.format(date)).toList
    
        println(res1) // List(2020_04_02, 2020_04_03, 2020_04_04, 2020_04_05, 2020_04_06)
        println(res2) // List(2020_04_02, 2020_04_03, 2020_04_04, 2020_04_05, 2020_04_06 ... 2020_05_31, 2020_06_01, 2020_06_02)
    
    
    

    【讨论】:

      【解决方案2】:

      我对此并不感到非常自豪,但我以前也这样做过:

      val start = LocalDate.of(2020,1,1).toEpochDay
      val end = LocalDate.of(2020,12,31).toEpochDay
      val dates = (start to end).map(LocalDate.ofEpochDay(_).toString).toArray
      

      你最终得到:

      dates: Array[String] = Array(2020-01-01, 2020-01-02, ..., 2020-12-31)
      

      【讨论】:

        【解决方案3】:

        此函数将返回 List[String]。

        import java.time.format.DateTimeFormatter
        import java.time.LocalDate
        
          val dt1 = "2020_04_02"
          val dt2 = "2020_04_06"
        
          def DatesBetween(startDate: String,endDate: String) : List[String] = {
            def ConvertToFormat = DateTimeFormatter.ofPattern("yyyy_MM_dd")
            val sdate = LocalDate.parse(startDate,ConvertToFormat)
            val edate = LocalDate.parse(endDate,ConvertToFormat)
            val DateRange = sdate.toEpochDay.until(edate.plusDays(1).toEpochDay).map(LocalDate.ofEpochDay).toList
            val ListofDateRange = DateRange.map(date => ConvertToFormat.format(date)).toList
            ListofDateRange
          }
        
          println(DatesBetween(dt1,dt2))
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-07-25
          • 1970-01-01
          • 1970-01-01
          • 2013-02-05
          • 2017-04-22
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多