Authors Top

If you have a few years of experience with the Kotlin language and server-side development, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

1. Overview

Converting strings to date types is one of the most common tasks. Modern programming languages and libraries offer several ways to achieve this.

In this tutorial, we’ll explore some efficient ways to convert strings into various date types with Kotlin.

2. String to LocalDate and LocalDateTime

First, we’ll see how to parse strings to LocalDate and LocalDateTime types.

2.1. Parse String to LocalDate

The LocalDate class has static parse methods to parse strings. The first method we’ll see takes one parameter that should follow the standard date format yyyy-MM-dd:

fun givenString_whenDefaultFormat_thenLocalDateCreated() {
    val localDate = LocalDate.parse("2022-01-06")

The second static method is an overloaded method that takes two parameters. In this case, the second parameter receives the date format of our String:

fun givenString_whenCustomFormat_thenLocalDateCreated() {
    val localDate = LocalDate.parse("01-06-2022", DateTimeFormatter.ofPattern("MM-dd-yyyy"))

2.2. Parse String to LocalDateTime

The LocalDateTime class defines the same two static methods to parse Strings but considers the time information. The one-parameter method receives Strings that should be in ISO_LOCAL_DATE_TIME format:

fun givenString_whenDefaultFormat_thenLocalDateTimeCreated() {
    val localDateTime = LocalDateTime.parse("2022-01-06T21:30:10")

Similarly, as defined for LocalDate, the second method takes the format parameter for any other date-time pattern:

fun givenString_whenCustomFormat_thenLocalDateTimeCreated() {
    val text = "2022-01-06 20:30:45"
    val pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
    val localDateTime = LocalDateTime.parse(text, pattern)


3. String to java.util.Date

Sometimes, we need to work with the old API java.util.Date. Fortunately, since Kotlin has excellent compatibility with Java, we can use the SimpleDateFormat class.

Let’s see now how to convert Strings into Date objects of type java.util.Date.

3.1. Parse String to java.util.Date

First, we need to define the formatting object that we’ll use for the date pattern. Then, we’ll use it to parse the String:

fun givenString_whenParseDate_thenUtilDateIsCreated() {
    val formatter = SimpleDateFormat("yyyy-MM-dd")
    val text = "2022-01-06"
    val date = formatter.parse(text)


4. Parsing String with Time Zone

4.1. Parse String with Time Zone Using ZonedDateTime

LocalDate and LocalDateTime objects are timezone agnostic. However, if we need to work with specific time-zone dates, the class ZonedDateTime can be used to parse the Strings:

fun givenString_whenParseWithTimeZone_thenZonedDateTimeIsCreated() {
    val text = "2022-01-06 20:30:45 America/Los_Angeles"
    val pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z")
    val zonedDateTime = ZonedDateTime.parse(text, pattern)


4.2. Parse java.util.Date with Time Zone

Similarly, java.util.Date can also provide the time zone information using the formatting object:

fun givenString_whenParseDateWithTimeZone_thenUtilDateIsCreated() {
    val formatter = SimpleDateFormat("yyyy-MM-dd")
    formatter.timeZone = TimeZone.getTimeZone("America/Los_Angeles");

    val text = "2022-01-06"
    val date = formatter.parse(text)


5. Conclusion

In this tutorial, we’ve seen how to convert String objects into Date types using the Date-Time API with Kotlin. We also saw how to parse Strings with the java.util.Date API.

As always, the complete code for this article is available over on GitHub.

Authors Bottom

If you have a few years of experience with the Kotlin language and server-side development, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!