Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

java.time Kotlin extension functions library.

License

NotificationsYou must be signed in to change notification settings

seljabali/java-time-fun

Repository files navigation

SwiftDate

Java Time Fun

Java Time Kotlin extension functions.

Build StatusMaven CentralKotlinKotlin Weekly


- 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) {- val zoneId = ZoneId.of("America/Los_Angeles")+ val zoneId = ZoneIds.AMERICA_LOS_ANGELES

Features

Parsing

Convert strings into Java Time objects with ease

val result="01:30 AM".toLocalTime()val result="2021-06-07".toLocalDate()val result="06/07/2021".toLocalDate(format="MM/dd/yyyy")val result="2024-11-15T12:34:56.123456Z".toLocalDateTime()// handles fractional seconds that Java Time doesn'tval result="2021-10-04T10:10:00+0000".toZonedDateTime()

Creation

Create new date/time instances using factory functions

val result=LocalTimeFactory.new(hour=7, minute=30)val result=LocalDateFactory.new(year=2024, month=11, day=15)val result=LocalDateTimeFactory.new(year=2024, month=11, day=15)val result=ZonedDateTimeFactory.new(year=2024, month=11, day=15, zoneId=ZoneIds.AMERICA_LOS_ANGELES)

Conversion from Legacy Date Types

Easily convert legacy date objects to Java Time

val result=Date().toLocalDateTime()val result=GregorianCalendar().toZonedDateTime()

Comparisons

Compare dates and times at various granularities

// Yearval result= dateA.compareYear(dateB)val result= dateA.isBeforeYear(dateB)// Monthval result= dateA.compareMonth(dateB)val result= zonedDateA.getMonthDifference(zonedDateB)// auto-conversion to same time zone for expected resultsval result= dateA.isEqualMonth(dateB)// Dayval result= dateA.compareDay(dateB)val result= dateA.getDayDifference(dateB)val result= dateA.isAfterEqualDay(dateB)// Timeval result= dateA.compareTime(dateB)val result= dateA.getMinuteDifference(dateB)val result= dateA.isAfterEqualTime(dateB)

Formatting

Print dates and times using a custom format

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

Attributes & Mutations

Query and transform date/time attributes

val result= date.isAtStartOfDay()val result= date.getDaysInMonth()val result= date.atStartOfDay()val result= date.getLast(DayOfWeek.FRIDAY)val result= date.getNext(DayOfWeek.MONDAY)

Preset Dates

Quickly access commonly used dates

val result=LocalDates.startOfYear()val result=LocalDateTimes.tomorrow()val result=ZonedDateTimes.nextMonday()

Installation

Add the following to your module’sbuild.gradle:

repositories {  mavenCentral()}dependencies {  implementation("org.eljabali.sami.javatimefun:javatimefun:4.0.1")}
For Android

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

  • Ensure you're usingGradle Plugin 4.0.0+.
  • Update modulebuild.gradle:
android {    defaultConfig {// Required when setting minSdkVersion to 20 or lower        multiDexEnabledtrue    }    compileOptions {// Flag to enable support for the new language APIs        coreLibraryDesugaringEnabledtrue// Sets Java compatibility to Java 8        sourceCompatibilityJavaVersion.VERSION_1_8        targetCompatibilityJavaVersion.VERSION_1_8    }}dependencies {    coreLibraryDesugaring'com.android.tools:desugar_jdk_libs:1.1.5'}

For more information on Android desugaring clickhere.

Find this library useful? 😏

If you like what you see, please star the repositoryas others have! ⭐️

Contributors4

  •  
  •  
  •  
  •  

Languages


[8]ページ先頭

©2009-2025 Movatter.jp