How to convert string date in Kotlin

I have a date of string type "2020-08-10". How to convert my string date to this format Monday 08 2020 in Kotlin?


Solution 1:

Code:

var parsedDate = LocalDate.parse("2020-08-10", DateTimeFormatter.ofPattern("yyyy-MM-dd"))
println("2020-08-10 : "+parsedDate.dayOfWeek.toString()+" "+parsedDate.monthValue+" "+parsedDate.year)

Output:

2020-08-10 : MONDAY 8 2020

For API 26 Below:

val parser =  SimpleDateFormat("yyyy-MM-dd")
val formatter = SimpleDateFormat("EEEE MM yyyy")
val formattedDate = formatter.format(parser.parse("2020-08-10"))
println("2020-08-10 : "+formattedDate)

Output:

2020-08-10 : MONDAY 8 2020

Solution 2:

The EEEE prints the name of day

import java.time.LocalDate
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

val str = "2020-08-10"
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")    
val dateTime = LocalDate.parse(str, formatter)
    
println(dateTime.format(DateTimeFormatter.ofPattern("EEEE MM yyyy ")))

Output

 Monday 08 2020

It's an alternative solution to your question.