Issue
I have succedded to format LocalDateTime to String but now how can I do the same for a list ?
The first function works but the second one doesn't work.
Here my class
class DateFormat {
companion object {
const val PATTERN = "dd-MM-yyyy HH:mm:ss"
fun format(date: LocalDateTime): String {
val formatter = DateTimeFormatter.ofPattern(PATTERN)
return date.format(formatter)
}
fun formatList(date: List<LocalDateTime>): String {
val formatter = DateTimeFormatter.ofPattern(PATTERN)
return date.forEach {
format(formatter)
}
}
}
}
Solution
to return a list of strings you could do this
fun formatList(date: List<LocalDateTime>): List<String> {
val formatter = DateTimeFormatter.ofPattern(PATTERN)
return date.map { it.format(formatter) }
}
or even shorter by referring to your other function like this:
fun formatList(date: List<LocalDateTime>): List<String> {
return date.map { format(it)}
}
or even this to make it super short
fun formatList(date: List<LocalDateTime>) = date.map { format(it)}
Edit: realized you could even write this
fun formatList(date: List<LocalDateTime>) = date.map(::format)
Answered By - Ivo Beckers
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.