Convert string into LocalDate Kotlin

Viewed 7023

I want to convert string value this is my code like

val dateFirst = "20 Aug 2012"
val dateSecond = "12/16/2020 12:00:00 AM"

val dateFirstConverted = LocalDate.parse(dateFirst, DateTimeFormatter.BASIC_ISO_DATE)
val dateSecondConverted = LocalDate.parse(dateSecond, DateTimeFormatter.BASIC_ISO_DATE)

println(dateFirstConverted)
println(dateSecondConverted)

then i get error like this.

Exception in thread "main" java.time.format.DateTimeParseException: Text '20 Aug 2012' could not be parsed at index 0
    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 App.TryKt.main(try.kt:11)
    at App.TryKt.main(try.kt)

can someone help me how to fix this ?

2 Answers

you have problem because the format of the date is not supported, I invite you to read this article https://www.claudebueno.com/programmation/comment-gerer-la-date-et-lheure-avec-kotlin.htm but in your case if you want that the code runs, change the format of date, like this:

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

fun main() {
    //example
    val current = LocalDateTime.now()
    val formatter = DateTimeFormatter.BASIC_ISO_DATE
    val formatted = current.format(formatter)
    println("Current Date is: $formatted")
    
    //your code
    val dates = /*"20 Aug 2012"*/ "20120820"
    val datess = LocalDate.parse(dates, DateTimeFormatter.BASIC_ISO_DATE)
    println(datess)
}

tl;dr ⇒ You are using the wrong pattern for parsing

  1. Your date String is of the format dd MMM uuuu (a non ISO format) but you are trying to parse it with a DateTimeFormatter.ISO_LOCAL_DATE

  2. Your datetime String is of the format MM/dd/uuuu hh:mm:ss a (non ISO) but you are trying to parse it with a DateTimeFormatter.ISO_LOCAL_DATE, which is at least doubly wrong because that formatter tries to parse an ISO date. Your String is non ISO and contains more information (time of day) than this formatter is able to parse.

There are several built-in DateTimeFormatters, like the one you are currently using, but you need to use a correct one or if there is none, create one that covers your String(s) yourself (either by DateTimeFormatter.ofPattern(...) or by using a DateTimeFormatterBuilder).

Here's a small example for your String examples:

fun main(args: Array<String>) {
    // your example Strings
    val dateFirst = "20 Aug 2012"
    val dateSecond = "12/16/2020 12:00:00 AM"
    // you need two different formatters here, your Strings differ in format and content
    val firstFormatter = DateTimeFormatter.ofPattern("dd MMM uuuu", Locale.ENGLISH)
    val secondFormatter = DateTimeFormatter.ofPattern("MM/dd/uuuu hh:mm:ss a", Locale.ENGLISH)
    // then use those according to what you want to parse
    val localDate = LocalDate.parse(dateFirst, firstFormatter)
    val localDateTime = LocalDateTime.parse(dateSecond, secondFormatter)
    // use the built-in formatters for output
    println(localDate.format(DateTimeFormatter.ISO_LOCAL_DATE))
    println(localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME))
}

Output (in ISO):

2012-08-20
2020-12-16T00:00:00
Related