java.time Kotlin extension functions library.

Overview

SwiftDate

Java Time Kotlin extension functions.

Background

Java Time became integrated to the JDK as of Java 8. It was a huge improvement over its Date predecessor.

Java Time Fun library empowers Java Time even more, making usage of dates & times a breeze.

What's In It?

1. Parsing

// Provided time
val result = "01:30 AM".parseLocalTime()

// Provided local date
val result = "2021-06-07".parseLocalDate()

// Provided ambiguous date formats
val result = "06/07/2021".parseLocalDate(format = "MM/dd/yyyy")

// Automatic time zone conversions
val result = "2021-10-04T10:10:00+0000".parseZonedDateTime()

// Maintain original time zone
val result = "2021-10-04T10:10:00+0000".parseZonedDateTime(useSystemTimeZone = false)

// Parse LocalDate as ZonedDateTime
val result = "2021-06-07".parseZonedDateTime()

2. Creation

val result = ZonedDateTimeUtil.new(year = 2021, month = 3, day = 25)

val result = LocalDateTimeUtil.new(Date())

val result = LocalDateUtil.new(GregorianCalendar())

val result = LocalTimeUtil.new(hour = 5, minute = 30)

3. Comparisons

// Year
val result = dateA.compareYear(dateB)
val result = dateA.isBeforeYear(dateB)

// Month
val result = dateA.compareMonth(dateB)
val result = dateA.getMonthDifference(dateB)
val result = dateA.isEqualMonth(dateB)

// Day
val result = dateA.compareDay(dateB)
val result = dateA.getDayDifference(dateB)
val result = dateA.isAfterEqualDay(dateB)

// Time
val result = dateA.compareTime(dateB)
val result = dateA.getMinuteDifference(dateB)
val result = dateA.isAfterEqualTime(dateB)

4. Print

val date = "2021-07-06".parseZonedDateTime()
val result = date.print(format = "MM/dd/yyyy")

5. Attributes

val result = date.isAtStartOfDay()

val result = date.getDaysInMonth()

6. Mutations

val result = date.atStartOfDay()

val result = date.getLast(DayOfWeek.FRIDAY)

val result = date.getNext(DayOfWeek.MONDAY)

7. Preset Dates

val result = ZonedDateTimes.today

val result = LocalDateTimes.tomorrow

val result = LocalDates.nextMonday

How to install?

Add to root build.gradle:

allprojects {
  repositories {
    maven { url 'https://jitpack.io' } // last line
  }
}

Add to module build.gradle:

dependencies {
  implementation 'com.github.seljabali:java-time-fun:0.5'
}  

For Android

In addition to the above, you need to desugar your module:

  • Ensure you're using Gradle Plugin 4.0.0+.
  • Update module build.gradle:
android {
    defaultConfig {
        // Required when setting minSdkVersion to 20 or lower
        multiDexEnabled true
    }

    compileOptions {
        // Flag to enable support for the new language APIs
        coreLibraryDesugaringEnabled true
        // Sets Java compatibility to Java 8
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
}

For more information on Android desugaring click here.

You might also like...
Kmpcalendar - A calendar library and views written for kotlin multiplatform
Kmpcalendar - A calendar library and views written for kotlin multiplatform

KMPCalendarView Minimal Kotlin Multiplatform project with SwiftUI, Jetpack Compo

Android library for better Picker DialogFragments
Android library for better Picker DialogFragments

DialogFragments modeled after the AOSP Clock and Calendar apps to improve UX for picking time, date, numbers, and other things.

Android Week View is an android library to display calendars (week view or day view) within the app. It supports custom styling.
Android Week View is an android library to display calendars (week view or day view) within the app. It supports custom styling.

Android Week View Android Week View is an android library to display calendars (week view or day view) within the app. It supports custom styling. Fea

An android library which provides a compact calendar view much like the one used in google calenders.
An android library which provides a compact calendar view much like the one used in google calenders.

CompactCalendarView CompactCalendarView is a simple calendar view which provides scrolling between months. It's based on Java's Date and Calendar clas

CustomizableCalendar is a library that allows you to create your calendar, customizing UI and behaviour
CustomizableCalendar is a library that allows you to create your calendar, customizing UI and behaviour

CustomizableCalendar This library allows you to create a completely customizable calendar. You can use CustomizableCalendar to create your calendar, c

android library dialog month picker
android library dialog month picker

RackMonthPicker android library dialog month picker Download Download via Maven: Add the JitPack repository to your build file repositories rep

A simple library which gives you custom design CalendarView with dialog functionality and event handlers.
A simple library which gives you custom design CalendarView with dialog functionality and event handlers.

CalendarView A simple library which gives you custom design CalendarView with dialog functionality and event handlers. 1: CalendarView Demo Screen 1.1

A Jetpack Compose library for handling calendar component rendering.
A Jetpack Compose library for handling calendar component rendering.

Compose Calendar Compose Calendar is a composable handling all complexity of rendering calendar component and date selection. Due to flexibility provi

📅 Minimal Calendar - This calendar library is built with jetpack compose. Easy, simple, and minimal.
📅 Minimal Calendar - This calendar library is built with jetpack compose. Easy, simple, and minimal.

📅 Minimal Calendar This calendar library is built with jetpack compose. Easy, simple, and minimal. Latest version The stable version of the library i

Comments
  • Add more new ZonedDateTime methods

    Add more new ZonedDateTime methods

    • Add more new ZonedDateTime methods:
      • Using Calendar.
      • Using Date.
      • Capability to default to systemTimeZone or leave as UTC.
    • Refactor to use YearUtil.
    • Add JavaDoc documentation.
    opened by seljabali 0
Releases(0.5)
Owner
Sami Eljabali
Sami Eljabali
Android NTP time library. Get the true current time impervious to device clock time changes

TrueTime for Android Make sure to check out our counterpart too: TrueTime, an NTP library for Swift. NTP client for Android. Calculate the date and ti

Instacart 1.3k Jan 4, 2023
Multiplatform Date and time library for Kotlin

Klock is a Date & Time library for Multiplatform Kotlin. It is designed to be as allocation-free as possible using Kotlin inline classes, to be consis

null 681 Dec 19, 2022
Additions for Kotlin's date & time library kotlinx-datetime

fluid-time Additions for Kotlin's date & time library kotlinx-datetime. kotlinx-datetime is very early stage and not as actively developed as other of

Marc Knaup 39 Nov 11, 2022
Joda-Time library with Android specialization

Android has built-in date and time handling - why bother with a library? If you've worked with Java's Date and Calendar classes you can probably answer this question yourself, but if not, check out Joda-Time's list of benefits.

Daniel Lew 2.6k Dec 9, 2022
KotlinX multiplatform date/time library

kotlinx-datetime A multiplatform Kotlin library for working with date and time. See Using in your projects for the instructions how to setup a depende

Kotlin 1.6k Jan 5, 2023
A material-styled android view that provisions picking of a date, time & recurrence option, all from a single user-interface.

SublimePicker A customizable view that provisions picking of a date, time & recurrence option, all from a single user-interface. You can also view 'Su

Vikram 2.3k Jan 4, 2023
Pick a date or time on Android in style

Material DateTime Picker - Select a time/date in style Material DateTime Picker tries to offer you the date and time pickers as shown in the Material

null 4.7k Dec 29, 2022
Estimated Time of Arrival Bar

Estimated Time of Arrival Bar

Tek 1 Mar 12, 2022
Asimov-flagz-kt - Feature flags library based on Togglz library

Asimov Flagz Feature flags library based on Togglz library. Installation Gradle

Nicolas Bottarini 1 Jan 8, 2022
A Kotlin Multiplatform library for working with dates and times

Island Time A Kotlin Multiplatform library for working with dates and times, heavily inspired by the java.time library. Features: A full set of date-t

Erik Christensen 71 Dec 28, 2022