如果日期在Scala的两个日期之间,我怎样才能阻止

时间:2016-07-27 05:05:48

标签: scala date

如何在两个日期之间获取日期?

这是我的约会对象:

val dateFrom = "01 Jan, 1970"
val dateTill = "01 Jan, 2016"

日期范围:

def getLocalDate(date: String): LocalDate = {

  LocalDate.parse(date, format.DateTimeFormatter.ofPattern("dd MMM, yyyy"))
}

def isDateBetWeenRange(from: String, till: String, date: String): Boolean = {

  val fromDate = getLocalDate(from)
  val tillDate = getLocalDate(till)
  val myDate = getLocalDate(date)

  myDate.isBefore(tillDate) && myDate.isAfter(fromDate) 
}

我想查看我的日期是否在日期范围之间。

这就是我的尝试:

val date = "01 Jan, 2010"
println(isDateBetWeenRange("01 Jan, 2000", "01 Jan, 2016", "01 Jan, date))

但是,如果日期与直到日期相同,则返回false:

  <input type="email" name="email" id= value"" required>    <br>

2 个答案:

答案 0 :(得分:0)

添加一些行为的好处和Scala方法是为LocalDate添加一个隐式类

例如:

implicit class SLocalDate(val time: LocalDate) {
  def isBeforeEq(other: ChronoLocalDate) = !time.isAfter(other)
  def isAfterEq(other: ChronoLocalDate) = !time.isBefore(other)
}

现在,在此隐含的范围内,您可以将测试更改为:

myDate.isBeforeEq(tillDate) && myDate.isAfterEq(fromDate)

答案 1 :(得分:0)

你可以调整你的功能:

(myDate.isBefore(tillDate) && myDate.isAfter(fromDate)) || 
  myDate.isEqual(tillDate) || 
  myDate.isEqual(fromDate)