Exercise: Multiplatform LocalDateTime
In your multiplatform project, you need to define a common type to represent time. You decided that you want it to behave just like LocalDateTime
from the java.time
library. In fact, you decided that on Kotlin/JVM you want to use LocalDateTime
directly as an actual type. Define a common type LocalDateTime
; then, define an actual typealias for Kotlin/JVM and an actual class for Kotlin/JS that wraps over Date
from JavaScript.
These are the expected elements that you need to provide for each platform:
expect class LocalDateTime {
fun getSecond(): Int
fun getMinute(): Int
fun getHour(): Int
fun plusSeconds(seconds: Long): LocalDateTime
}
expect fun now(): LocalDateTime
expect fun parseLocalDateTime(str: String): LocalDateTime
Starting code and unit tests for this exercise can be found in the project:
Once you are done with the exercise, you can check your solution here.
Marcin Moskala is a highly experienced developer and Kotlin instructor as the founder of Kt. Academy, an official JetBrains partner specializing in Kotlin training, Google Developers Expert, known for his significant contributions to the Kotlin community. Moskala is the author of several widely recognized books, including "Effective Kotlin," "Kotlin Coroutines," "Functional Kotlin," "Advanced Kotlin," "Kotlin Essentials," and "Android Development with Kotlin."
Beyond his literary achievements, Moskala is the author of the largest Medium publication dedicated to Kotlin. As a respected speaker, he has been invited to share his insights at numerous programming conferences, including events such as Droidcon and the prestigious Kotlin Conf, the premier conference dedicated to the Kotlin programming language.