Kotlin Program To Convert String to Date

1. Introduction


In this article, you'll learn how to convert String to Date in Kotlin. The string can be any valid date format but you should convert the String date format to an actual date object.

Example: 

String = "2020-05-01";
Converted Date: 2020-05-01 

A converted date is in a form of the Date object.

This is part of the Kotin Programming Series.


Kotlin Program To Convert String to Date



2.  Kotlin - Convert String to Date using Predefined Built-in Patterns


[package com.javaprogramto.kotlin.stringtodate
import java.time.LocalDate
import java.time.format.DateTimeFormatter
fun main() {
var dateInString = "2020-05-02" var simpleFormat = DateTimeFormatter.ISO_DATE;
var convertedDate = LocalDate.parse(dateInString, simpleFormat)
println("String to Date converted : $convertedDate")
}]

Output:

[String to Date converted : 2020-05-02]

In the above program first created String that stores the data value and created a default simple date format DateTimeFormatter.ISO_DATE that takes the data in formats "YYYY-MM-DD" or "YYYY-MM-DD+HH:SS".

LocalDate.parse() method takes two values and those are date in the string and the format.

Finally, the parse() method returns a LocalDate object that will have the same value of the string date.

3. Kotlin - Convert String to Date Other Formats using Predefined Formats


In the above program, you have seen converting a string to date in the same format. But, you will see the conversion from one format string to a different date format.

Example to covert "YYYY-MM-DD+HH:SS" to "YYYY-MM-DD".

[var dateInString1 = "2020-12-03+01:00";
var simpleFormat2 = DateTimeFormatter.ISO_DATE;
var output = LocalDate.parse(dateInString1, simpleFormat2)
println("output : "+output)]

Output:

[output : 2020-12-03]

4. Kotlin - Convert String to Date Using Custom Pattern Formatters


There is another way to do convert string to date using custom patterns. Always dates will not be in predefined formats provided in DateTimeFormatter class.

Sometimes, you will be needed for different date formats are in a string. You should tell to the parse() method what is the string date format. Otherwise, we will get an exception.

[package com.javaprogramto.kotlin.dates.stringtodate
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.util.*
fun main() {
val string = "May 31, 2020"

val formatter = DateTimeFormatter.ofPattern("MMM dd, yyyy", Locale.ENGLISH)
val date = LocalDate.parse(string, formatter)
println("Date in format of MMM dd, yyyy is $date")
}]

Output:

[Formated Date object is 2020-05-31]

5. Kotlin Date Format Exceptions


As a result, If you pass incorrect date formates to the parse() method then it will throw DateTimeParseException.

 Support/IntelliJIdea2019.2/Kotlin/kotlinc/lib/kotlin-stdlib.jar:/Users/venkateshn/Library/Application Support/IntelliJIdea2019.2/Kotlin/kotlinc/lib/kotlin-reflect.jar:/Users/venkateshn/Library/Application Support/IntelliJIdea2019.2/Kotlin/kotlinc/lib/kotlin-test.jar" com.javaprogramto.kotlin.stringtodate.KotlinStringToDateKt
Exception in thread "main" java.time.format.DateTimeParseException: Text '2020=05-02' could not be parsed at index 4

at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)

at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)

at java.time.LocalDate.parse(LocalDate.java:400)

at com.javaprogramto.kotlin.stringtodate.KotlinStringToDateKt.main(KotlinStringToDate.kt:12)

at com.javaprogramto.kotlin.stringtodate.KotlinStringToDateKt.main(KotlinStringToDate.kt)


Process finished with exit code 1

Exception 2:

Exception in thread "main" java.time.format.DateTimeParseException: Text 'May 31, 2020' could not be parsed, unparsed text found at index 10

at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1952)

at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)

at java.time.LocalDate.parse(LocalDate.java:400)

at com.javaprogramto.kotlin.dates.stringtodate.KotlinStringToDateCustomFormatsKt.main(KotlinStringToDateCustomFormats.kt:14)

at com.javaprogramto.kotlin.dates.stringtodate.KotlinStringToDateCustomFormatsKt.main(KotlinStringToDateCustomFormats.kt)

6. Conclusion


In conclusion, you've seen how to convert string to date in kotlin using predefined formats and custom formats.

All the code is shown in this article is over GitHub.

You can download the project directly and can run in your local without any errors.


[View on GitHub ##eye##]

[Download ##file-download##]


If you have any queries please post in the comment section.

0 Comments