Created
December 11, 2019 13:38
-
-
Save todokr/371346eaf7ea02c3f239ccbd20bb13f7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.time.LocalDate | |
val today = LocalDate.now | |
println(s"today is $today") | |
/** 2つの指定日とその間の日からなるLocalDateのシーケンス(昇順)を返す */ | |
def rangeDays(from: LocalDate, to: LocalDate): Seq[LocalDate] = | |
Iterator.iterate(from)(_.plusDays(1)).takeWhile(!_.isAfter(to)).toSeq | |
rangeDays(today, today.plusDays(5)).foreach(println) | |
// => List(2017-09-11, 2017-09-12, 2017-09-13, 2017-09-14, 2017-09-15, 2017-09-16) | |
/** 指定日を含むn日間のLocalDateのシーケンス(昇順)を返す */ | |
def listDays(from: LocalDate, days: Int): Seq[LocalDate] = | |
Iterator.tabulate(days)(i => from.plusDays(i)).toSeq | |
listDays(today, 3).foreach(println) | |
// => List(2017-09-11, 2017-09-12, 2017-09-13) | |
/** ↑にステップを導入したもの */ | |
def listDaysWithStep(from: LocalDate, days: Int, step: Int): Seq[LocalDate] = | |
Iterator.range(0, days, step).map(from.plusDays(_)).toSeq | |
listDaysWithStep(today, 10, 3).foreach(println) | |
// => List(2017-09-11, 2017-09-14, 2017-09-17, 2017-09-20) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment