I need to obtain a collection that should be filled with datetime intervals between some input and LocalDatetime.now.
Example:
If I would to build a interval based on every 30 minutes, it should be something like 2017-06-12 00:30, 2017-06-12 01:00, 2017-06-12 01:30, 2017-06-12 02:00..and go on.
I've wrote a code that does my requirement but it looks like Java using Scala syntax - not a functional and cleaner way.
val now = LocalDateTime.now
val dates = if (interval.toInt > 0) {
var tempDate = LocalDate.now.atStartOfDay()
val allDates = scala.collection.mutable.ListBuffer[String]()
while (tempDate.isBefore(now)) {
allDates += tempDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"))
tempDate = tempDate.plusMinutes(interval.toInt)
}
allDates
} else {
var initialDay = LocalDate.now.minusDays(30)
val allDates = scala.collection.mutable.ListBuffer[String]()
while (initialDay.isBefore(LocalDate.now)) {
allDates += initialDay.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))
initialDay = initialDay.plusDays(1)
}
allDates
}
How can I refactor this code to looks like functional style or at least remove that mutable variable?