Convert String to Date Using Pattern Formatters

Cover - Kotlin

In this tutorial, we will talk about converting String to Date using Pattern Formatters.

In this tutorial, we will talk about converting String to Date using Pattern Formatters.

Example: Convert String to Date using Patter Formatters

import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.util.*

fun main(args: Array<String>){
    val dateString3 = "September 29, 2019"
    val formatterEnglish = DateTimeFormatter.ofPattern("MMMM d, yyyy", Locale.ENGLISH)
    val dateFormatEnglish = LocalDate.parse(dateString3, formatterEnglish)
    println("Date Pattern English: $dateFormatEnglish")
}

When you run the program above, the output will be:

Date Pattern English: 2019-09-29

In the above program, the DateTimeFormatter is in the format MMM d, yyyy. DateTimeFormatter is used for printing and parsing date-time objects. Then, we parse the date using LocalDate.parse() function and get the LocalDate object. That’s all for this tutorial. For another tutorial, you can find it through this link.

Close