def getDateRange(startTime:String,endTime:String) = {
val dateFormat = new SimpleDateFormat("yyyy-MM-dd")
val dateFiled = Calendar.DAY_OF_MONTH
var beginDate = dateFormat.parse(startTime)
val endDate = dateFormat.parse(endTime)
val calendar = Calendar.getInstance()
calendar.setTime(beginDate)
val dateArray : ArrayBuffer[String] = ArrayBuffer()
while(beginDate.compareTo(endDate) <= 0){
dateArray += dateFormat.format(beginDate)
calendar.add(dateFiled, 1)
beginDate = calendar.getTime
}
dateArray.toList
val dateFormat = new SimpleDateFormat("yyyy-MM-dd")
val dateFiled = Calendar.DAY_OF_MONTH
var beginDate = dateFormat.parse(startTime)
val endDate = dateFormat.parse(endTime)
val calendar = Calendar.getInstance()
calendar.setTime(beginDate)
val dateArray : ArrayBuffer[String] = ArrayBuffer()
while(beginDate.compareTo(endDate) <= 0){
dateArray += dateFormat.format(beginDate)
calendar.add(dateFiled, 1)
beginDate = calendar.getTime
}
dateArray.toList
}
测试:
def main(args: Array[String]) {
val index = getDateRange("2017-05-20","2017-06-01")
println("两个时间相隔所有天:"+index.mkString(","))
}
结果: