Hasan A Yousef
Hasan A Yousef

Reputation: 24938

Working with Date and LocalDate

I want to use an array of data class where one of the parameters is Date, I could not use Date() as it looks to be related to KotlinJS, so I tried using LocalDate so I wrote the below code:

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

data class transaction (var date: LocalDate, var quantity: Double)
val formatter: DateTimeFormatter
    get() = DateTimeFormatter.ofPattern("dd.mm.yyyy", Locale.ENGLISH)

fun main(args: Array<String>) {
    var salesOrders = ArrayList<transaction>()

    println("Hello, world! ")

    salesOrders.set(0, transaction(LocalDate.parse("01.02.2018", formatter), 0.0))


    println(salesOrders)

}

But, as shown at tryKotlin I'm getting an error, that:

Text '01.02.2018' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {Year=2018, MinuteOfHour=2, DayOfMonth=1}.

Upvotes: 2

Views: 5369

Answers (2)

leonardkraemer
leonardkraemer

Reputation: 6783

it must be MM not mm see DateTimeFormatter

Upvotes: 0

yole
yole

Reputation: 97138

As described in the DateTimeFormatter documentation, the "m" character in the pattern means "minute"; "month" is "M". You need to change "mm" to "MM".

Upvotes: 4

Related Questions