Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/main/kotlin/javatimefun/localdate/LocalDateUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package javatimefun.localdate

import java.time.LocalDate
import java.time.Month
import java.util.Date
import java.util.Calendar

/**
* Contains helper functions that only serve to create new LocalDates.
Expand Down
44 changes: 18 additions & 26 deletions src/main/kotlin/javatimefun/localdate/LocalDates.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,25 @@ import java.time.LocalDate

object LocalDates {
val today: LocalDate get() = LocalDate.now()
val yesterday: LocalDate get() = javatimefun.localdate.LocalDates.today.minusDays(1)
val tomorrow: LocalDate get() = javatimefun.localdate.LocalDates.today.plusDays(1)
val yesterday: LocalDate get() = today.minusDays(1)
val tomorrow: LocalDate get() = today.plusDays(1)

val lastMonday: LocalDate get() = javatimefun.localdate.LocalDates.today.getLast(DayOfWeek.MONDAY)
val lastTuesday: LocalDate get() = javatimefun.localdate.LocalDates.today.getLast(DayOfWeek.TUESDAY)
val lastWednesday: LocalDate get() = javatimefun.localdate.LocalDates.today.getLast(DayOfWeek.WEDNESDAY)
val lastThursday: LocalDate get() = javatimefun.localdate.LocalDates.today.getLast(DayOfWeek.THURSDAY)
val lastFriday: LocalDate get() = javatimefun.localdate.LocalDates.today.getLast(DayOfWeek.FRIDAY)
val lastSaturday: LocalDate get() = javatimefun.localdate.LocalDates.today.getLast(DayOfWeek.SATURDAY)
val lastSunday: LocalDate get() = javatimefun.localdate.LocalDates.today.getLast(DayOfWeek.SUNDAY)
val lastMonday: LocalDate get() = today.getLast(DayOfWeek.MONDAY)
val lastTuesday: LocalDate get() = today.getLast(DayOfWeek.TUESDAY)
val lastWednesday: LocalDate get() = today.getLast(DayOfWeek.WEDNESDAY)
val lastThursday: LocalDate get() = today.getLast(DayOfWeek.THURSDAY)
val lastFriday: LocalDate get() = today.getLast(DayOfWeek.FRIDAY)
val lastSaturday: LocalDate get() = today.getLast(DayOfWeek.SATURDAY)
val lastSunday: LocalDate get() = today.getLast(DayOfWeek.SUNDAY)

val nextMonday: LocalDate get() = javatimefun.localdate.LocalDates.today.getNext(DayOfWeek.MONDAY)
val nextTuesday: LocalDate get() = javatimefun.localdate.LocalDates.today.getNext(DayOfWeek.TUESDAY)
val nextWednesday: LocalDate get() = javatimefun.localdate.LocalDates.today.getNext(DayOfWeek.WEDNESDAY)
val nextThursday: LocalDate get() = javatimefun.localdate.LocalDates.today.getNext(DayOfWeek.THURSDAY)
val nextFriday: LocalDate get() = javatimefun.localdate.LocalDates.today.getNext(DayOfWeek.FRIDAY)
val nextSaturday: LocalDate get() = javatimefun.localdate.LocalDates.today.getNext(DayOfWeek.SATURDAY)
val nextSunday: LocalDate get() = javatimefun.localdate.LocalDates.today.getNext(DayOfWeek.SUNDAY)
val nextMonday: LocalDate get() = today.getNext(DayOfWeek.MONDAY)
val nextTuesday: LocalDate get() = today.getNext(DayOfWeek.TUESDAY)
val nextWednesday: LocalDate get() = today.getNext(DayOfWeek.WEDNESDAY)
val nextThursday: LocalDate get() = today.getNext(DayOfWeek.THURSDAY)
val nextFriday: LocalDate get() = today.getNext(DayOfWeek.FRIDAY)
val nextSaturday: LocalDate get() = today.getNext(DayOfWeek.SATURDAY)
val nextSunday: LocalDate get() = today.getNext(DayOfWeek.SUNDAY)

val firstDayOfThisYear: LocalDate get() = javatimefun.localdate.LocalDateUtil.new(
javatimefun.localdate.LocalDates.today.year,
1,
1
)
val lastDayOfThisYear: LocalDate get() = javatimefun.localdate.LocalDateUtil.new(
javatimefun.localdate.LocalDates.today.year,
12,
31
)
val firstDayOfThisYear: LocalDate get() = LocalDateUtil.new(today.year, 1, 1)
val lastDayOfThisYear: LocalDate get() = LocalDateUtil.new(today.year, 12, 31)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package javatimefun.localdate.extensions

import java.time.Duration
import java.time.LocalDate
import java.time.temporal.ChronoUnit

// region Day Comparisons
fun LocalDate.compareDay(toDate: LocalDate): Int {
Expand Down Expand Up @@ -74,13 +75,20 @@ fun LocalDate.isAfterEqualYear(localDateB: LocalDate): Boolean = this.year >= lo
fun LocalDate.isAfterYear(localDateB: LocalDate): Boolean = this.year > localDateB.year
// endregion

fun LocalDate.getMinuteDifference(localDateB: LocalDate): Long = Duration.between(this, localDateB).toMinutes()
fun LocalDate.getSecondDifference(localDateB: LocalDate): Long =
Duration.between(this.atStartOfDay(), localDateB.atStartOfDay()).toSeconds()

fun LocalDate.getHourDifference(localDateB: LocalDate): Long = Duration.between(this, localDateB).toHours()
fun LocalDate.getMinuteDifference(localDateB: LocalDate): Long =
Duration.between(this.atStartOfDay(), localDateB.atStartOfDay()).toMinutes()

fun LocalDate.getDayDifference(localDateB: LocalDate): Long = Duration.between(this.atStartOfDay(), localDateB.atStartOfDay()).toDays()
fun LocalDate.getHourDifference(localDateB: LocalDate): Long =
Duration.between(this.atStartOfDay(), localDateB.atStartOfDay()).toHours()

fun LocalDate.getMonthDifference(localDateB: LocalDate): Int {
val yearDif = (localDateB.year - this.year) * 12
return yearDif + (localDateB.month.value - this.month.value)
}
fun LocalDate.getDayDifference(localDateB: LocalDate): Long =
ChronoUnit.DAYS.between(this, localDateB)

fun LocalDate.getMonthDifference(localDateB: LocalDate): Long =
ChronoUnit.MONTHS.between(this, localDateB)

fun LocalDate.getYearDifference(localDateB: LocalDate): Long =
ChronoUnit.YEARS.between(this, localDateB)
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package javatimefun.localdatetime

import javatimefun.date.extensions.toLocalDateTime
import java.time.Instant
import java.time.LocalDateTime
import java.time.Month
import java.time.ZoneOffset
import java.util.Date

/**
* Contains helper functions that only serve to create new LocalDateTimes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package javatimefun.localdatetime.extensions

import java.time.Duration
import java.time.LocalDateTime
import java.time.temporal.ChronoUnit

// region Day Comparisons
fun LocalDateTime.compareDay(toDate: LocalDateTime): Int {
Expand Down Expand Up @@ -61,17 +62,23 @@ fun LocalDateTime.isAfterMonth(localDateTimeB: LocalDateTime): Boolean {
// endregion

// region Year Comparisons
fun LocalDateTime.compareYear(localDateTimeB: LocalDateTime): Int = this.year.compareTo(localDateTimeB.year)
fun LocalDateTime.compareYear(localDateTimeB: LocalDateTime): Int =
this.year.compareTo(localDateTimeB.year)

fun LocalDateTime.isBeforeYear(localDateTimeB: LocalDateTime): Boolean = this.year < localDateTimeB.year
fun LocalDateTime.isBeforeYear(localDateTimeB: LocalDateTime): Boolean =
this.year < localDateTimeB.year

fun LocalDateTime.isBeforeEqualYear(localDateTimeB: LocalDateTime): Boolean = this.year <= localDateTimeB.year
fun LocalDateTime.isBeforeEqualYear(localDateTimeB: LocalDateTime): Boolean =
this.year <= localDateTimeB.year

fun LocalDateTime.isEqualYear(localDateTimeB: LocalDateTime): Boolean = this.year == localDateTimeB.year
fun LocalDateTime.isEqualYear(localDateTimeB: LocalDateTime): Boolean =
this.year == localDateTimeB.year

fun LocalDateTime.isAfterEqualYear(localDateTimeB: LocalDateTime): Boolean = this.year >= localDateTimeB.year
fun LocalDateTime.isAfterEqualYear(localDateTimeB: LocalDateTime): Boolean =
this.year >= localDateTimeB.year

fun LocalDateTime.isAfterYear(localDateTimeB: LocalDateTime): Boolean = this.year > localDateTimeB.year
fun LocalDateTime.isAfterYear(localDateTimeB: LocalDateTime): Boolean =
this.year > localDateTimeB.year
// endregion

// region Time Comparisons
Expand All @@ -93,6 +100,9 @@ fun LocalDateTime.isAfterTime(b: LocalDateTime): Boolean = this.compareTime(b) >
fun LocalDateTime.isAfterEqualTime(b: LocalDateTime): Boolean = this.compareTime(b) >= 0
// endregion

fun LocalDateTime.getSecondDifference(localDateTimeB: LocalDateTime): Long =
Duration.between(this, localDateTimeB).toSeconds()

fun LocalDateTime.getMinuteDifference(localDateTimeB: LocalDateTime): Long =
Duration.between(this, localDateTimeB).toMinutes()

Expand All @@ -102,7 +112,8 @@ fun LocalDateTime.getHourDifference(localDateTimeB: LocalDateTime): Long =
fun LocalDateTime.getDayDifference(localDateTimeB: LocalDateTime): Long =
Duration.between(this.atStartOfDay(), localDateTimeB.atStartOfDay()).toDays()

fun LocalDateTime.getMonthDifference(localDateTimeB: LocalDateTime): Int {
val yearDif = (localDateTimeB.year - this.year) * 12
return yearDif + (localDateTimeB.month.value - this.month.value)
}
fun LocalDateTime.getMonthDifference(localDateTimeB: LocalDateTime): Long =
ChronoUnit.MONTHS.between(this, localDateTimeB)

fun LocalDateTime.getYearDifference(localDateTimeB: LocalDateTime): Long =
ChronoUnit.YEARS.between(this, localDateTimeB)
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@ fun LocalTime.isAfterTime(b: LocalTime): Boolean = compareTime(b) > 0
fun LocalTime.isAfterEqualTime(b: LocalTime): Boolean = compareTime(b) >= 0
// endregion

fun LocalTime.getSecondDifference(localTimeB: LocalTime): Int = Duration.between(this, localTimeB).seconds.toInt()
fun LocalTime.getMilliSecondDifference(localTimeB: LocalTime): Int =
Duration.between(this, localTimeB).toMillis().toInt()

fun LocalTime.getMinuteDifference(localTimeB: LocalTime): Int = Duration.between(this, localTimeB).toMinutes().toInt()
fun LocalTime.getSecondDifference(localTimeB: LocalTime): Int =
Duration.between(this, localTimeB).seconds.toInt()

fun LocalTime.getHourDifference(localTimeB: LocalTime): Int = Duration.between(this, localTimeB).toHours().toInt()
fun LocalTime.getMinuteDifference(localTimeB: LocalTime): Int =
Duration.between(this, localTimeB).toMinutes().toInt()

fun LocalTime.getHourDifference(localTimeB: LocalTime): Int =
Duration.between(this, localTimeB).toHours().toInt()
21 changes: 18 additions & 3 deletions src/main/kotlin/javatimefun/zoneddatetime/ZonedDateTimeUtil.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package javatimefun.zoneddatetime

import java.time.*
import java.time.Instant
import java.time.LocalDateTime
import java.time.Month
import java.time.ZoneId
import java.time.ZoneOffset
import java.time.ZonedDateTime

/**
* Contains helper functions that only serve to create new ZonedDateTimes.
Expand Down Expand Up @@ -29,7 +34,8 @@ object ZonedDateTimeUtil {
nano: Int = 0,
useSystemTimeZone: Boolean = true
): ZonedDateTime {
val localDateTime = LocalDateTime.of(year, Month.of(month), day, hourIn24, minute, second, nano)
val localDateTime =
LocalDateTime.of(year, Month.of(month), day, hourIn24, minute, second, nano)
return ZonedDateTime.of(
localDateTime,
if (useSystemTimeZone) ZoneId.systemDefault() else ZoneOffset.UTC
Expand Down Expand Up @@ -58,7 +64,16 @@ object ZonedDateTimeUtil {
nano: Int = 0,
isAm: Boolean,
useSystemTimeZone: Boolean = true
): ZonedDateTime = new(year, month, day, if (isAm) hour else hour + 12, minute, second, nano, useSystemTimeZone)
): ZonedDateTime = new(
year,
month,
day,
if (isAm) hour else hour + 12,
minute,
second,
nano,
useSystemTimeZone
)

/**
* @param epochMilliseconds Epoch time, aka Unix time, are seconds elapsed since January 1st 1970 at 00:00:00 UTC.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package javatimefun.zoneddatetime.extensions

import java.time.Duration
import java.time.ZonedDateTime
import java.time.temporal.ChronoUnit

// region Year Comparisons
/**
Expand Down Expand Up @@ -155,35 +156,40 @@ fun ZonedDateTime.compareDay(zonedDateTimeB: ZonedDateTime): Int {
* @param zonedDateTimeB ZonedDateTime of which we want to compare context's day to.
* @return True if context's equals param's day, false otherwise.
*/
fun ZonedDateTime.isEqualDay(zonedDateTimeB: ZonedDateTime): Boolean = this.compareDay(zonedDateTimeB) == 0
fun ZonedDateTime.isEqualDay(zonedDateTimeB: ZonedDateTime): Boolean =
this.compareDay(zonedDateTimeB) == 0

/**
* Works off of ZonedDateTime context.
* @param zonedDateTimeB ZonedDateTime of which we want to compare context's day to.
* @return True if context is before param's day, false otherwise.
*/
fun ZonedDateTime.isBeforeDay(zonedDateTimeB: ZonedDateTime): Boolean = this.compareDay(zonedDateTimeB) < 0
fun ZonedDateTime.isBeforeDay(zonedDateTimeB: ZonedDateTime): Boolean =
this.compareDay(zonedDateTimeB) < 0

/**
* Works off of ZonedDateTime context.
* @param zonedDateTimeB ZonedDateTime of which we want to compare context's day to.
* @return True if context is before or equal param's day, false otherwise.
*/
fun ZonedDateTime.isBeforeEqualDay(zonedDateTimeB: ZonedDateTime): Boolean = this.compareDay(zonedDateTimeB) <= 0
fun ZonedDateTime.isBeforeEqualDay(zonedDateTimeB: ZonedDateTime): Boolean =
this.compareDay(zonedDateTimeB) <= 0

/**
* Works off of ZonedDateTime context.
* @param zonedDateTimeB ZonedDateTime of which we want to compare context's day to.
* @return True if context is after param's day, false otherwise.
*/
fun ZonedDateTime.isAfterDay(zonedDateTimeB: ZonedDateTime): Boolean = this.compareDay(zonedDateTimeB) > 0
fun ZonedDateTime.isAfterDay(zonedDateTimeB: ZonedDateTime): Boolean =
this.compareDay(zonedDateTimeB) > 0

/**
* Works off of ZonedDateTime context.
* @param zonedDateTimeB ZonedDateTime of which we want to compare context's day to.
* @return True if context is after or equal param's day, false otherwise.
*/
fun ZonedDateTime.isAfterEqualDay(zonedDateTimeB: ZonedDateTime): Boolean = this.compareDay(zonedDateTimeB) >= 0
fun ZonedDateTime.isAfterEqualDay(zonedDateTimeB: ZonedDateTime): Boolean =
this.compareDay(zonedDateTimeB) >= 0
// endregion

// region Time Comparisons
Expand Down Expand Up @@ -211,30 +217,44 @@ fun ZonedDateTime.isEqualTime(zonedDateTimeB: ZonedDateTime): Boolean = this.isE
* @param zonedDateTimeB ZonedDateTime of which we want to compare context's day & time to.
* @return True if context is before param's day & time, false otherwise.
*/
fun ZonedDateTime.isBeforeTime(zonedDateTimeB: ZonedDateTime): Boolean = this.isBefore(zonedDateTimeB)
fun ZonedDateTime.isBeforeTime(zonedDateTimeB: ZonedDateTime): Boolean =
this.isBefore(zonedDateTimeB)

/**
* Works off of ZonedDateTime context.
* @param zonedDateTimeB ZonedDateTime of which we want to compare context's day & time to.
* @return True if context is before or equal param's day & time, false otherwise.
*/
fun ZonedDateTime.isBeforeEqualTime(zonedDateTimeB: ZonedDateTime): Boolean = this.compareTime(zonedDateTimeB) <= 0
fun ZonedDateTime.isBeforeEqualTime(zonedDateTimeB: ZonedDateTime): Boolean =
this.compareTime(zonedDateTimeB) <= 0

/**
* Works off of ZonedDateTime context.
* @param zonedDateTimeB ZonedDateTime of which we want to compare context's day & time to.
* @return True if context is after param's day & time, false otherwise.
*/
fun ZonedDateTime.isAfterTime(zonedDateTimeB: ZonedDateTime): Boolean = this.compareTime(zonedDateTimeB) > 0
fun ZonedDateTime.isAfterTime(zonedDateTimeB: ZonedDateTime): Boolean =
this.compareTime(zonedDateTimeB) > 0

/**
* Works off of ZonedDateTime context.
* @param zonedDateTimeB ZonedDateTime of which we want to compare context's day & time to.
* @return True if context is after or equal param's day & time, false otherwise.
*/
fun ZonedDateTime.isAfterEqualTime(zonedDateTimeB: ZonedDateTime): Boolean = this.compareTime(zonedDateTimeB) >= 0
fun ZonedDateTime.isAfterEqualTime(zonedDateTimeB: ZonedDateTime): Boolean =
this.compareTime(zonedDateTimeB) >= 0
// endregion

/**
* Works off of ZonedDateTime context.
* @param zonedDateTimeB ZonedDateTime of which we want to compare context's second difference from.
* @return Seconds away from param be it positive or negative.
*/
fun ZonedDateTime.getSecondDifference(zonedDateTimeB: ZonedDateTime): Long {
val otherZonedDateTime = zonedDateTimeB.withTimeZoneOf(this)
return Duration.between(this, otherZonedDateTime).toSeconds()
}

/**
* Works off of ZonedDateTime context.
* @param zonedDateTimeB ZonedDateTime of which we want to compare context's minute difference from.
Expand Down Expand Up @@ -270,8 +290,17 @@ fun ZonedDateTime.getDayDifference(zonedDateTimeB: ZonedDateTime): Long {
* @param zonedDateTimeB ZonedDateTime of which we want to compare context's months difference from.
* @return Months away from param be it positive or negative.
*/
fun ZonedDateTime.getMonthDifference(zonedDateTimeB: ZonedDateTime): Int {
fun ZonedDateTime.getMonthDifference(zonedDateTimeB: ZonedDateTime): Long {
val otherZonedDateTime = zonedDateTimeB.withTimeZoneOf(this)
return ChronoUnit.MONTHS.between(this, otherZonedDateTime)
}

/**
* Works off of ZonedDateTime context.
* @param zonedDateTimeB ZonedDateTime of which we want to compare context's years difference from.
* @return Years away from param be it positive or negative.
*/
fun ZonedDateTime.getYearDifference(zonedDateTimeB: ZonedDateTime): Long {
val otherZonedDateTime = zonedDateTimeB.withTimeZoneOf(this)
val yearDif = (otherZonedDateTime.year - this.year) * 12
return yearDif + (otherZonedDateTime.month.value - this.month.value)
return ChronoUnit.YEARS.between(this, otherZonedDateTime)
}
Loading