Working with Date

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}.

You have to use “dd.MM.yyyy” for the date format. “mm” is for minutes while “MM” is a month.

1 Like

Thanks, is there a way to use Date instead of LocalDate

The Date class for KotlinJs is just an “interface” for this js implementation. So no there is no way to use it on the jvm. As long as you are compiling to jvm bytecode you have to use the java libraries.

1 Like