kotlin extensions to the java8 time library

Related tags

Kotlin ktime
Overview

ktime

Kotlin extensions to the java8 time library

Examples

Conversion from numbers to Duration

Int, Long and Double values can be suffixed with a time unit to create a Duration

val delay = 30.seconds              // equal to Duration.ofSeconds(30)
val longDelay = 2.5.minutes         // equal to Duration.ofSeconds(150)

Available Duration suffixes: nanos, micros, millis, seconds, minutes, hours

Conversion from numbers to Period

Int and Long values can be suffixed with a date unit to create a Period

val interval = 30L.days

Available Period suffixes: days, weeks, months, years

Mathematical operations on Duration and Period

addition, subtraction, negation

val duration = 2.hours + 30.minutes
val period = 2.months + 14.days            
val duration = 1.hours - 1.minutes
val negativeDuration = -30.seconds

Scaling

val fullDuration = 2.5.hours
val doubledDuration = 2.5.hours * 2
val halfDuration = 2.5.hours / 2

Comparisons on Duration

Note that Period is not Comparable

1.hours > 1.minutes
1000.nanos == 1.micros    

Destructuring

val (sec, nano) = 2.5.seconds
val (y, m, d) = 1.years + 2.months + 3.days

Offsets from now

These produce an OffsetDateTime with a UTC offset:

val lastHour = 1.hours.ago
val thisTimeTomorrow = 1.days.fromNow

Operations on times and dates

Add or subtract Duration or Period from date/time types

val startTime = 30.seconds.fromNow
val stopTime = startTime + 1.minutes                                 // add Durations & Periods to dates & times

TemporalQueries

Most TemporalQueries involve extracting a data type that contains a subset of the data for the current data type. For example, you can go from OffsetDateTime to LocalDateTime, but not vice-versa because LocalDateTime doesn't have zoneOffset.

val thisYear: Year = 0.seconds.ago.year
val thisMonth: Month = 0.seconds.ago.month
val dayOfWeek: DayOfWeek = 0.seconds.ago.dayOfWeek
val currentTime: localTime = 0.seconds.ago.localTime

TemporalAdjusters

val startOfThisMonth = 0.seconds.ago.atStartOfThisMonth
val startOfNextMonth = 0.seconds.ago.atStartOfNextMonth
val endOfThisMonth = 0.seconds.ago.atEndOfThisMonth
val startOfThisYear = 0.seconds.ago.atStartOfThisYear
val startOfNextYear = 0.seconds.ago.atStartOfNextYear
val endOfThisYear = 0.seconds.ago.atEndOfThisYear

Temporal Ranges

Although java 8's time library doesn't support the idea of a Range, this library does. In addition to offering a step modifier, these ranges also offer a unit modifier. The unit modifier is used to specify the unit (MINUTES, DAYS, etc.) to use if you iterate over the range. If no unit is specified, the native precision is used (e.g. the native precision for a date is DAYS, the native precision for DateTimes is NANOS)

Inclusive ranges

 (2010.asYear..2020.asYear).count() // should be equal to 11

 val inOfferPeriod = 0.seconds.ago in offer.start..offer.end
 
 for (d in date1..date2) {...}
 for (d in time1..time2 step 10 unit MINUTES) {...}
 for (d in dateTime1..dateTime2 unit DAYS) {...}

Exclusive ranges

 (2010.asYear until 2020.asYear).count() // should be equal to 10
 for (d in date1 until date2) {...}
 for (d in time1 until time2 step 10 unit MINUTES) {...}
 for (d in dateTime1 until dateTime2 unit DAYS) {...}
You might also like...
A fork of our clean architecture boilerplate, this time using the Android Architecture Components

Android Clean Architecture Components Boilerplate Note: This is a fork of our original Clean Architecture Boilerplate, except in this repo we have swi

The Superhero app keeps track of the near real-time location of all the volunteers.

Superhero The Superhero app keeps track of the near real-time location of all the volunteers. The app sends requests for emergency tasks to nearby vol

Clean MVVM with eliminating the usage of context from view models by introducing hilt for DI and sealed classes for displaying Errors in views using shared flows (one time event), and Stateflow for data

Clean ViewModel with Sealed Classes Following are the purposes of this repo Showing how you can remove the need of context in ViewModels. I. By using

Add Wheel Date - Time Picker in Android Jetpack Compose.
Add Wheel Date - Time Picker in Android Jetpack Compose.

WheelPickerCompose Add Wheel Date - Time Picker in Android Jetpack Compose. Usage Picker Usage WheelDateTimePicker { snappedDateTime - } WheelDatePic

Run Kotlin/JS libraries in Kotlin/JVM and Kotlin/Native programs

Zipline This library streamlines using Kotlin/JS libraries from Kotlin/JVM and Kotlin/Native programs. It makes it possible to do continuous deploymen

A somewhat copy past of Jetbrain's code from the kotlin plugin repo to make it humanly possible to test Intellij IDEA kotlin plugins that work on kotlin

A somewhat copy past of Jetbrain's code from the kotlin plugin repo to make it humanly possible to test Intellij IDEA kotlin plugins that work on kotlin

Real life Kotlin Multiplatform project with an iOS application developed in Swift with SwiftUI, an Android application developed in Kotlin with Jetpack Compose and a backed in Kotlin hosted on AppEngine.

Conferences4Hall Real life Kotlin Multiplatform project with an iOS application developed in Swift with SwiftUI, an Android application developed in K

[Android Library] A SharedPreferences helper library to save and fetch the values easily.

Preference Helper A SharedPreferences helper library to save and fetch the values easily. Featured in Use in your project Add this to your module's bu

Kotlin library for Android
Kotlin library for Android

KAndroid Kotlin library for Android providing useful extensions to eliminate boilerplate code in Android SDK and focus on productivity. Download Downl

Owner
Hank Davidson
Hank Davidson
WorkManager ,One time,Sequential Execution, Periodic time Execution

WokManagerSample WorkManager ,One time,Sequential Execution, Periodic time Execu

Chhote Lal Pal 0 Dec 21, 2021
A collection of hand-crafted extensions for your Kotlin projects.

Splitties Splitties is a collection of small Kotlin multiplatform libraries (with Android as first target). These libraries are intended to reduce the

Louis CAD 2.2k Dec 25, 2022
Common Android/Kotlin extensions

Common Android/Kotlin extensions Gradle implementation "com.github.javokhirsavriev:common-extensions:1.0.1" License Copyright 2022 Javokhir Savriev L

Javokhir 0 Feb 15, 2022
Various experimental proposals and extensions to Javalin 4.x used in Reposilite 3.x

Javalin RFCs Various experimental extensions to Javalin 4.x used in Reposilite 3.x. Provides basic support for Kotlin coroutines and async routes with

Reposilite Playground 5 Feb 22, 2022
Automatic CoroutineDispatcher injection and extensions for kotlinx.coroutines

Dispatch Utilities for kotlinx.coroutines which make them type-safe, easier to test, and more expressive. Use the predefined types and factories or de

Rick Busarow 132 Dec 9, 2022
Android View Lifecycle Extensions

Android View Lifecycle Extensions Extensions for Android View class that let you access a view lifecycle without having to create a custom view (exten

Thomas Gorisse 22 Dec 6, 2022
Actions are things that run, with parameters. Serves as a common dependency for a variety of Cepi extensions.

Actions Actions that take in customizable paramaters, an optional target, and do things. Installation Download the jar from Releases OR compile it you

Cepi 1 Jan 9, 2022
A Kotlin Native program to show the time since a date, using Kotlin LibUI

TimeSince A Kotlin Native program to show the time since a date, using Kotlin LibUI Report Bug . Request Feature About The Project TimeSince is a Kotl

Russell Banks 2 May 6, 2022
Type-safe time calculations in Kotlin, powered by generics.

Time This library is made for you if you have ever written something like this: val duration = 10 * 1000 to represent a duration of 10 seconds(in mill

Kizito Nwose 958 Dec 10, 2022
Utility for developers and QAs what helps minimize time wasting on writing the same data for testing over and over again. Made by Stfalcon

Stfalcon Fixturer A Utility for developers and QAs which helps minimize time wasting on writing the same data for testing over and over again. You can

Stfalcon LLC 31 Nov 29, 2021