Skip to content

sandjelkovic/kxjtime

Repository files navigation

Maven Central pipeline status Build Status

KxjTime

KxjTime is a micro library that provides a convenient way to create java.time API objects.

Usage

Simplify creation of various objects from java.time API.

Extensions for Integers and Longs

Create Duration from Integers

val thirtyMinutes = 30.minutes
val twoMilliseconds = 2.milliseconds

Which can be then combined by Kotlin overloaded operators or with infix and

val duration1 = 10.days + 1.hours + 5.minutes + 3.seconds
val duration2 = 10.days and 1.hours and 5.minutes and 3.seconds

Natural language

Combine Duration with Time-based classes

val createdAt = 10.days and 1.hours and 5.minutes before ZonedDateTime.now()
val willDoSomethingAt = 30.minutes + 10.seconds + 3.hours after Instant.now()

Readable and concise code

if (createdAt.isBefore(30.minutes + 10.seconds + 3.hours after now)) {
    // do something
}
if (now.isAfter(12.hours after scheduledTime)) {
    // do something later
}

Deprecation note

With the recent releases of Kotlin time library this library is not needed anymore if you use newer Kotlin versions