The Wayback Machine - https://web.archive.org/web/20210905064738/https://github.com/seljabali/java-time-fun
Skip to content
main
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.

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, convert to ZonedDateTime using system time zone
val result = "2021-06-07".parseZonedDateTime()

2. Creation

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

val result = ZonedDateTimeUtil.new(1325134800000)

3. Comparisons

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

// 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)

// 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 at the end of repositories:

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

Add to module build.gradle

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

For Android

In addition to the above, you need 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.