An intuitive Date extensions in Kotlin.

Related tags

UI/UX khronos
Overview

khronos

Build Status Download License

An intuitive Date extensions in Kotlin.

Usage

Add durations to date

val today = Dates.today
val nextWeek = today + 1.week
val dayBeforeYesterday = today - 2.days

// shortcuts #1
val tomorrow = Dates.tomorrow
val yesterday = Dates.yesterday

// shortcuts #2
val yesterday = 1.days.ago
val fiveYearsSince = 5.years.since

Initialize by specifying date components

val birthday = Dates.of(year = 1990, month = 1, day = 21)
val firstCommitDate = Dates.of(year = 2016, month = 2, day = 26, hour = 18, minute = 58, second = 31, millisecond = 777)

Initialize by changing date components

val today = Dates.today
val christmas = today.with(month = 12, day = 25)
val thisSunday = today.with(weekday = 1)

// shortcuts
val newYearDay = today.beginningOfYear
val newYearsEve = today.endOfYear

Check day of the week

Dates.today.isFriday() // false

Format and parse

5.minutes.since.toString("yyyy-MM-dd HH:mm:ss")
//=> "2015-03-01 12:05:00"

"1987-06-02".toDate("yyyy-MM-dd")
//=> Dates.of(year = 1987, month = 6, day = 2)

Compare dates

1.day.ago > 2.days.ago // true
1.day.ago in 2.days.ago..Dates.today // true

Install

${latest.version} = Download

dependencies {
  compile 'com.github.hotchemi:khronos:${latest.version}'
}

Notice

Licence

Copyright 2016 Shintaro Katafuchi

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Comments
  • Add support for milliseconds

    Add support for milliseconds

    Hi there!

    Java Date are precise to milliseconds so I think it would be a good idea for this library to support milliseconds.

    Otherwise, you don't have any way of creating two equal dates:

    val date     = Dates.of(year = 2016, month = 2, day = 26, hour = 18, minute = 58, second = 31)
    val sameDate = Dates.of(year = 2016, month = 2, day = 26, hour = 18, minute = 58, second = 31)
    date.equals(sameDate) // => return false!
    

    This PR allow this library to creates two equal dates:

    val date     = Dates.of(year = 2016, month = 2, day = 26, hour = 18, minute = 58, second = 31, millisecond = 777)
    val sameDate = Dates.of(year = 2016, month = 2, day = 26, hour = 18, minute = 58, second = 31, millisecond = 777)
    date.equals(sameDate) // => return true
    

    I think there is no breaking changes and all unit tests should pass. Don't hesitate to ask me for changes, I really like this library but I need to compare dates equality so I need this changes :)

    opened by debona 6
  • Dates.now is not now

    Dates.now is not now

    Dates.now is expected to be the current time but instead it's the time that the application has started due to it being initialised as a static property. Shouldn't this return a new Date object every time it gets called?

    opened by erwinknoop 5
  • Support for milliseconds

    Support for milliseconds

    Add support for setting milliseconds, as epoch time in Java based on millis and not on seconds.

    @hotchemi The unit tests works on my machine, do you have any idea why they fail?

    opened by yoavst 5
  • "1.month" does not work correctly

    Due to the incorrect subtraction here the expression "1.month" returns a duration of 0 months. As a consequence, this obviously incorrect test passes: assertEquals(Dates.today, 1.month.ago)

    bug 
    opened by yole 3
  • Incorrect date comparison

    Incorrect date comparison

    import khronos.Dates.today
    import khronos.day
    
    fun main() {
    	val d = 0.day.ago
    	val n = today
    	println(d)
    	println(n)
    	println(d > n)
    	println(0.day.ago > today)
    }
    
    Mon Jan 06 14:29:48 MSK 2020
    Mon Jan 06 14:29:48 MSK 2020
    false
    true // incorrect - expect `false`
    
    opened by BorzdeG 1
  • Confusing 1.month.ago == today

    Confusing 1.month.ago == today

    Why 1.month is 0 based and is not consistent with other properties?

    This very confusing when 1.month.ago == today

        val monthAgo = 1.month.ago.month
        val today = Dates.today.month
        assert(monthAgo == today)
    
    bug waiting for approval 
    opened by awronski 1
  • Subtracting a month does not work

    Subtracting a month does not work

    #30

    This is a breaking change, but it is an improvement in terms of semantics. We should not take the 0-index ordering of months of the Java Date APIs as gospel here. 5.months should actually give us 5 months worth of duration to add to a Date.

    bug 
    opened by urgentx 0
  • Subtracting a month does not work

    Subtracting a month does not work

    fun main() {
    	val n = Dates.today
    	println(n)
    	println(n - 1.month)
    	println(1.month.ago)
    	println(n - 1.day)
    	println(1.day.ago)
    	println(1.week.ago)
    }
    

    output:

    Mon Jan 06 14:13:28 MSK 2020
    Mon Jan 06 14:13:28 MSK 2020 // expect December 2019
    Mon Jan 06 14:13:29 MSK 2020 // expect December 2019
    Sun Jan 05 14:13:28 MSK 2020
    Sun Jan 05 14:13:29 MSK 2020
    Sun Dec 30 14:13:29 MSK 2020
    

    khronos version: 0.9.0 JVM: 1.8 Kotlin: 1.3.61

    bug waiting for approval 
    opened by BorzdeG 0
  • Date compariossion

    Date compariossion

    I found a small problem when I was comparing dates.

    After some debugging I noticed that you used Date.with(...) where we added all date properties except MILLISECOND.

    fun Date.with(year: Int = -1, month: Int = -1, day: Int = -1, hour: Int = -1, minute: Int = -1, second: Int = -1): Date {
        calendar.time = this
        if (year > -1) calendar.set(Calendar.YEAR, year)
        if (month > 0) calendar.set(Calendar.MONTH, month - 1)
        if (day > 0) calendar.set(Calendar.DATE, day)
        if (hour > -1) calendar.set(Calendar.HOUR_OF_DAY, hour)
        if (minute > -1) calendar.set(Calendar.MINUTE, minute)
        if (second > -1) calendar.set(Calendar.SECOND, second)
    
        // NOTE : Added the next line. Used for compare
        calendar.set(Calendar.MILLISECOND, 0)
    
        return calendar.time
    }
    

    Problem is solved by adding the next line: calendar.set(Calendar.MILLISECOND, 0)

    This might vary for every person. You could also add it as a parameter.

    bug enhancement 
    opened by geertberkers 0
  • Dates.of() with one-biased day

    Dates.of() with one-biased day

    Day of month in Javas calendar starts with 1 not with 0. So the day -1 in Dates.kt is wrong.

    As there is also a Test it might be intended. But I think it is misleading to write Dates.of(day=21) and expect the calendar date to be 20.

    opened by martinbrylski 0
  • Multi-thread safe

    Multi-thread safe

    I like your library and it's very readable and use to fun. One thing I am wondering is if there is any multi-thread issue because your implementation has one instance.

    internal val calendar: Calendar by lazy { Calendar.getInstance(DateHelper.getSydneyTimeZone()) }

    From my understanding, lazy it doesn't create more than one instance and when thread1 and thread2 tries to update date, it think there could be a race condition. What do you think?

    enhancement good first issue 
    opened by krpot 2
  • N.days.ago.beginningOfDay.time does not return the same value for subsequent invocations.

    N.days.ago.beginningOfDay.time does not return the same value for subsequent invocations.

    30.days.ago.beginningOfDay.time

    The values are off by a few millis.

    1508437800629 1508437800649

    However Dates.today.endOfDay.time returns the same value for every invocation.

    opened by rjoncontract 1
Releases(0.9.0)
Provenance-eventstream-legacy-kotlin - A legacy Kotlin library for reading from the Provenance event stream

provenance-eventstream-legacy-kotlin A legacy Kotlin library for reading from th

Figure Technologies Inc. 1 Jan 13, 2022
Multiplatform UI DSL with screen management in common code for mobile (android & ios) Kotlin Multiplatform development

Mobile Kotlin widgets This is a Kotlin MultiPlatform library that provides declarative UI and application screens management in common code. You can i

IceRock Development 320 Dec 30, 2022
Kotlin/Native interop to libui: a portable GUI library

kotlin-libui Kotlin/Native bindings to the libui C library. libui is a C lightweight multi-platform UI library using native widgets on Linux (Gtk3), m

Mike Sinkovsky 611 Dec 30, 2022
Kotlin Wrapper Library of Material-UI

kotlin-material-ui Kotlin Wrapper Library of Material-UI Core: Lab: Installation repositories { jcenter() // or maven { url 'https://dl.bintra

Subroh Nishikori 78 Nov 24, 2022
Muirwik - a Material UI React wrapper written in Kotlin

Muirwik Welcome to Muirwik. Muirwik gets it name from being a Material UI React wrapper written in Kotlin. For more information, see the above links (

null 134 Nov 8, 2022
Have a look at Job Allocation app build with SQLite database and Kotlin support

Job Allocation Overview Do you remember or forget sometimes to whom you allocated a task ?? Have a look at Job Allocation app build with SQLite databa

null 0 Dec 13, 2021
Squishy button effect with kotlin

Squishy Usage Add squishy() modifier on your composable element. Text( text = "Squishhhhyyyyy", modifier = Modifier .size(200.dp, 100.dp

null 43 Jul 9, 2022
Car Shop App Built With Kotlin

Car Shop O aplicativo conta com 2 telas no total sendo elas a home(Tela principa

Luis Henrique 4 Dec 23, 2021
It is a Profile Image View with percentage progress developed in Kotlin.

It is a Profile Image View with percentage progress developed in Kotlin. It is a highly customizable view that offers multiple attributes for creating either dash or continuous progress view around profile image based on your requirements.

smartSense Solutions 16 Jun 23, 2022
ProfilePercentageView - A Profile Image View with percentage progress developed in Kotlin

ProfilePercentageView It is a Profile Image View with percentage progress develo

smartSense Solutions 8 Dec 31, 2021
Ms-goals - Project developed using Kotlin and Spring

Goals microservice Kotlin + Spring CRUD application. You can find the following

Gabriel Babler 0 Jan 28, 2022
Asimov-time-kt - Useful time and date related functions and extensions

asimov/time Useful time and date related functions and extensions. Installation

Nicolas Bottarini 1 Jan 7, 2022
Nepali Date Picker library in Jetpack compose for android with Date conversion from BS to AD and vice-versa

Nepali Date picker Converter - Re in Compose This is a re-work of Nepali Date Picker Converter in jetpack compose and kotlin. English Locale Nepali Lo

Kiran Gyawali 4 Dec 23, 2022
A nicer-looking, more intuitive and highly customizable alternative for radio buttons and dropdowns for Android.

SwipeSelector Undergoing for some API changes for a 2.0 major version, see example usage in the sample module! What and why? Bored of dull looking rad

Iiro Krankka 1.1k Dec 9, 2022
A nicer-looking, more intuitive and highly customizable alternative for radio buttons and dropdowns for Android.

SwipeSelector Undergoing for some API changes for a 2.0 major version, see example usage in the sample module! What and why? Bored of dull looking rad

Iiro Krankka 1.1k Dec 30, 2022
SwipeBack for Android Activities to do pretty the same as the android "back-button" will do, but in a really intuitive way by using a swipe gesture

SwipeBack SwipeBack is for Android Activities to do pretty the same as the android "back-button" will do, but in a really intuitive way by using a swi

Hannes Dorfmann 697 Dec 14, 2022
Fermata Media Player is a free, open source audio and video player with a simple and intuitive interface.

Fermata Media Player About Fermata Media Player is a free, open source audio and video player with a simple and intuitive interface. It is focused on

Andrey 227 Jan 6, 2023
💰 Expense Manager is simple, intuitive, stable and modern app that is just designed for you.

Expense Manager is simple, intuitive, stable and modern app that is just designed for you. Everything you need at your fingertips to manage the expenditures and budgets.

Behzod Bozorboev 13 Oct 20, 2022
🌨️ Simple, intuitive, and opinionated command handling library for Kord

??️ Snow Simple, intuitive, and opinionated command handling library for Kord Why? Since I maintain two Discord bots, both in Kotlin, Nino and Noel (p

Noel ʕ •ᴥ•ʔ 1 Jan 16, 2022
Dice 🎲 is a simple, intuitive, minimalistic and ad-free dice-roll application

Dice ?? Dice ?? is a simple, intuitive, minimalistic and ad-free dice-roll application ?? with which you can play your favourite board games, built to

Shreyansh Saurabh 5 Nov 15, 2022