How to convert a string to Date in Kotlin

Convert a string to a Date in Kotlin :

In this tutorial, we will learn how to convert a string to a date in Kotlin. We will use java.util.LocalDate class to convert a string to a Date.

java.util.LocalDate :

LocalDate represents a Date in ISO-8601 calendar system. Note that it represents a date without any time or timezone information. ISO-8601 uses the rule for leap year like proleptic Gregorian calendar system. If you are using it on Android application development, it is available only on API level 26 (Android 8.0, Oreo) and above.

Convert a simple date string :

LocalDate comes with one static method parse that takes one date string as input and formats it as LocalDate object. The syntax of this method is as below :

static fun parse(text: CharSequence!): LocalDate!

As you can see here, the method takes one CharSequence as the input parameter and returns the result LocalDate. One thing we need to keep in mind is that the input string should be a valid ISO-8601 date like 2018-02-13. If the date is not properly formatted, it will throw one DateTimeParseException.

Let’s try it with an example :

import java.time.LocalDate

fun main() {
    var parsedDate = LocalDate.parse("2018-11-10")
    println(parsedDate)
}

The output of the program is :

2018-11-10

kotlin string to date

Parsing with a formatter:

Instead of passing a string each time to convert it to a Date, we can also use one formatter to use different types of string. parse method with one extra argument is used for this. The syntax of this method is as below :

static fun parse(text: CharSequence!, formatter: DateTimeFormatter!): LocalDate!

Similar to the above example, the date to parse is passed as the first argument. The second argument is a DateTimeFormatter to use with the string. If the text can’t be parsed, it throws a DateTimeParseException.

Using this method, we can even convert complex strings like “December 2,2018”, 30/4/2018, etc. to a LocalDate object.

Let’s take one example with different use cases :

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

fun main() {
    var parsedDate = LocalDate.parse("Wednesday, November 21, 2018", DateTimeFormatter.ofPattern("EEEE, MMMM dd, yyyy"))
    println("Wednesday, November 21, 2018 : "+parsedDate)

    parsedDate = LocalDate.parse("December 21, 2018", DateTimeFormatter.ofPattern("MMMM dd, yyyy"))
    println("December 21, 2018 : "+parsedDate)

    parsedDate = LocalDate.parse("14/02/2018", DateTimeFormatter.ofPattern("dd/MM/yyyy"))
    println("14/02/2018 : "+parsedDate)

    parsedDate = LocalDate.parse("14 March,2011", DateTimeFormatter.ofPattern("dd MMMM,yyyy"))
    println("14 March,2011 : "+parsedDate)

    parsedDate = LocalDate.parse("19th April,2009", DateTimeFormatter.ofPattern("dd'th' MMMM,yyyy"))
    println("19th April,2009 : "+parsedDate)

    parsedDate = LocalDate.parse("19 Feb, 2001", DateTimeFormatter.ofPattern("dd MMM, yyyy"))
    println("19 Feb, 2001 : "+parsedDate)
}

It will print the below output :

Wednesday, November 21, 2018 : 2018-11-21
December 21, 2018 : 2018-12-21
14/02/2018 : 2018-02-14
14 March,2011 : 2011-03-14
19th April,2009 : 2009-04-19
19 Feb, 2001 : 2001-02-19

As you can see, we can pass different types of date-time with a Formatter to convert it to a LocalDate object.

Conclusion :

I hope that you have found something useful in this tutorial. Converting a string to LocalDate depends on what type of application you are building and it supports only ISO-8601 formatted string or not. Try to run the above examples, and drop one comment below if you have any questions.

You might also like: