This Kotlin example shows how to convert String to Date in Kotlin using formatter.
Convert String to Date using predefined formatters
package net.javaguides.kotlin.examples
import java.time.LocalDate
import java.time.format.DateTimeFormatter
fun main(args: Array<String>) {
val string = "2019-07-25"
val date = LocalDate.parse(string, DateTimeFormatter.ISO_DATE)
println(date)
}
Output:
2019-07-25
Convert String to Date using pattern formatters
package net.javaguides.kotlin.examples
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.util.Locale
fun main(args: Array<String>) {
val string = "July 25, 2019"
val formatter = DateTimeFormatter.ofPattern("MMMM d, yyyy", Locale.ENGLISH)
val date = LocalDate.parse(string, formatter)
println(date)
}
Output:
2019-07-25
Comments
Post a Comment