Home

Awesome

<p align="center" > <img src="screenshots/logo.png" width=300px alt="SwiftDate" title="SwiftDate"> </p> <h3 align="center"><strong>Java Time Kotlin extension functions.</strong></h3> <p align="center"> <a href="https://github.com/seljabali/java-time-fun/actions?query=branch%3Amain"><img alt="Build Status" src="https://github.com/seljabali/java-time-fun/actions/workflows/main.yml/badge.svg"/></a> <a href="https://repo1.maven.org/maven2/org/eljabali/sami/javatimefun/javatimefun/"><img alt="Maven Central" src="https://img.shields.io/maven-metadata/v?metadataUrl=https%3A%2F%2Frepo1.maven.org%2Fmaven2%2Forg%2Feljabali%2Fsami%2Fjavatimefun%2Fjavatimefun%2Fmaven-metadata.xml"/></a> <a href="https://kotlinlang.org"><img alt="Kotlin" src="https://img.shields.io/badge/Kotlin-1.9.20-orange.svg?style=flat&logo=kotlin"/></a> <a href="https://mailchi.mp/kotlinweekly/kotlin-weekly-396"><img alt="Kotlin Weekly" src="https://skydoves.github.io/badges/kotlin-weekly.svg"/></a> </p> <br>

Background

Java Time, released in Java 8, was a huge improvement over its Date predecessor.<br><br> This library empowers Java Time & makes it a lot of fun! 😃

- val dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd")
- val date = LocalDate.parse(dateText, dateTimeFormatter)
+ val date = dateText.toLocalDate("yyyyMMdd")


- val dateFormatter = DateTimeFormatter.ofPattern("MM/dd/yyyy")
- print(dateFormatter.format(date))
+ print(date.print("MM/dd/yyyy"))

- if (ChronoUnit.YEARS.between(dateOfBirth, LocalDate.now()) < 18) {
+ if (dateOfBirth.getYearDifference(LocalDates.today) < 18) {

What's In It?

1. Parsing

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

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

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

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

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

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

2. Creation

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

val result = Date().toLocalDateTime()

val result = GregorianCalendar().toLocalDate()

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".toZonedDateTime()
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

Install

Add to module build.gradle:

repositories {
  mavenCentral()
}

dependencies {
  implementation("org.eljabali.sami.javatimefun:javatimefun:3.0.0")
}  
<details> <summary>For Android</summary>

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

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.

</details>

Find this library useful? 😏

Star this repository as others have. ⭐️ <br>