将所有日期作为scala中给定月份和年份的列表

时间:2017-11-07 11:54:59

标签: scala

我厌倦了编写函数来将所有日期作为给定月份和年份的列表,如下所示

find()

我期望遵循列表

def allDaysForYear(year: String , month:String): List[(String, String, String)] = {
  val daysInYear = if(Year.of(year.toInt).isLeap) 366 else 365
  for {
    day <- (1 to daysInYear).toList
    localDate = LocalDate.ofYearDay(year.toInt, day)
    month = localDate.getMonthValue
    dayOfMonth = localDate.getDayOfMonth
  } yield (year, month.toString, dayOfMonth.toString)
}

任何人都可以帮忙

4 个答案:

答案 0 :(得分:3)

你很亲密。您已经拥有LocalDate.ofYearDay(year.toInt, day)的日期。

你只需要调用.toString,因为yield会产生你想要的序列:

def allDaysForYear(year: Int): Seq[String] = {
  val daysInYear = if(Year.of(year).isLeap) 366 else 365
  for(day <- 1 to daysInYear) yield LocalDate.ofYearDay(year, day).toString
}

答案 1 :(得分:0)

您可以使用java日历为您计算 看起来您想要格式化日期,所以我更改了签名

import java.util.Calendar
import java.text.SimpleDateFormat

val calendar = Calendar.getInstance
val formatter = new SimpleDateFormat("yyyy-MM-dd")

def allDaysForYear(year:Int, month:Int):List[String] = {
  calendar.set(year, month - 1, 1)

  (1 to calendar.getActualMaximum(Calendar.DAY_OF_MONTH)).map(_ => {
    val currentDate = calendar.getTime
    calendar.roll(Calendar.DATE, true)
    formatter.format(currentDate)
  }).toList
}

答案 2 :(得分:0)

我得到了这条底线,给定X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) 会返回一年中每一天的Year序列。

LocalDate

由调用者创建def datesInYear(year: Year) = (1 to year.length()).map(year.atDay) 。就像这样Year

这是如何工作的:Year.of(...)具有Year属性,可以处理leap年等。(不需要执行length)也不需要理解,如果我们正在做的是将一堆数字映射到日期。 if( leap ) 355 else 356接受一个Year#atDay并返回一个Int,这正是我们进行LocalDate调用所需的映射函数,因此无需为该映射构造匿名函数(即不需要map

答案 3 :(得分:0)

几种处理方法。以下代码也摆脱了leap年的显式处理。

如果您需要列表输出

def allDaysForYear(year: String , month:String): List[String] = {
val daysInMonth = java.time.YearMonth.of(year.toInt,month.toInt).lengthOfMonth
    for
    {
        day <- (1 to daysInMonth ).toList
        localdate = java.time.LocalDate.of(year.toInt,month.toInt,day).toString
    } yield(localdate)
}

2020年2月的产出看起来像这样

allDaysForYear("2020","02")

List[String] = List(2020-02-01, 2020-02-02, 2020-02-03, 2020-02-04, 2020-02-05, 2020-02-06, 2020-02-07, 2020-02-08, 2020-02-09, 2020-02-10, 2020-02-11, 2020-02-12, 2020-02-13, 2020-02-14, 2020-02-15, 2020-02-16, 2020-02-17, 2020-02-18, 2020-02-19, 2020-02-20, 2020-02-21, 2020-02-22, 2020-02-23, 2020-02-24, 2020-02-25, 2020-02-26, 2020-02-27, 2020-02-28, 2020-02-29)

如果需要序列

def allDaysForYear(year: String , month:String): Seq[String] = {
        val daysInMonth = java.time.YearMonth.of(year.toInt,month.toInt).lengthOfMonth
        for (day <- 1 to daysInMonth ) yield java.time.LocalDate.of(year.toInt,month.toInt,day).toString
}

2020年2月的产出看起来像这样

allDaysForYear("2020","02")

Seq[String] = Vector(2020-02-01, 2020-02-02, 2020-02-03, 2020-02-04, 2020-02-05, 2020-02-06, 2020-02-07, 2020-02-08, 2020-02-09, 2020-02-10, 2020-02-11, 2020-02-12, 2020-02-13, 2020-02-14, 2020-02-15, 2020-02-16, 2020-02-17, 2020-02-18, 2020-02-19, 2020-02-20, 2020-02-21, 2020-02-22, 2020-02-23, 2020-02-24, 2020-02-25, 2020-02-26, 2020-02-27, 2020-02-28, 2020-02-29)

如果在给定日期需要单行输出:

假设您的日期采用变量dt下的格式(“ YYYY-MM-DD”)或(“ YYYY-MM”)

val daterange = for (day <- 1 to java.time.YearMonth.of(dt.substring(0,4).toInt,dt.substring(5,7).toInt).lengthOfMonth ) yield java.time.LocalDate.of(dt.substring(0,4).toInt,dt.substring(5,7).toInt,day).toString

输出看起来像下面的变量

val dt = "2020-02" or for variable  val dt = "2020-02-01"/"2020-02-02"or any date for the month 

daterange: scala.collection.immutable.IndexedSeq[String] = Vector(2020-02-01, 2020-02-02, 2020-02-03, 2020-02-04, 2020-02-05, 2020-02-06, 2020-02-07, 2020-02-08, 2020-02-09, 2020-02-10, 2020-02-11, 2020-02-12, 2020-02-13, 2020-02-14, 2020-02-15, 2020-02-16, 2020-02-17, 2020-02-18, 2020-02-19, 2020-02-20, 2020-02-21, 2020-02-22, 2020-02-23, 2020-02-24, 2020-02-25, 2020-02-26, 2020-02-27, 2020-02-28, 2020-02-29)