Skip to content

Commit 643a390

Browse files
authored
Add Year Comparison Extensions (#30)
1 parent 9ef7f04 commit 643a390

File tree

12 files changed

+421
-73
lines changed

12 files changed

+421
-73
lines changed

src/main/kotlin/javatimefun/localdate/LocalDateUtil.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package javatimefun.localdate
22

33
import java.time.LocalDate
44
import java.time.Month
5-
import java.util.Date
6-
import java.util.Calendar
75

86
/**
97
* Contains helper functions that only serve to create new LocalDates.

src/main/kotlin/javatimefun/localdate/LocalDates.kt

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,25 @@ import java.time.LocalDate
77

88
object LocalDates {
99
val today: LocalDate get() = LocalDate.now()
10-
val yesterday: LocalDate get() = javatimefun.localdate.LocalDates.today.minusDays(1)
11-
val tomorrow: LocalDate get() = javatimefun.localdate.LocalDates.today.plusDays(1)
10+
val yesterday: LocalDate get() = today.minusDays(1)
11+
val tomorrow: LocalDate get() = today.plusDays(1)
1212

13-
val lastMonday: LocalDate get() = javatimefun.localdate.LocalDates.today.getLast(DayOfWeek.MONDAY)
14-
val lastTuesday: LocalDate get() = javatimefun.localdate.LocalDates.today.getLast(DayOfWeek.TUESDAY)
15-
val lastWednesday: LocalDate get() = javatimefun.localdate.LocalDates.today.getLast(DayOfWeek.WEDNESDAY)
16-
val lastThursday: LocalDate get() = javatimefun.localdate.LocalDates.today.getLast(DayOfWeek.THURSDAY)
17-
val lastFriday: LocalDate get() = javatimefun.localdate.LocalDates.today.getLast(DayOfWeek.FRIDAY)
18-
val lastSaturday: LocalDate get() = javatimefun.localdate.LocalDates.today.getLast(DayOfWeek.SATURDAY)
19-
val lastSunday: LocalDate get() = javatimefun.localdate.LocalDates.today.getLast(DayOfWeek.SUNDAY)
13+
val lastMonday: LocalDate get() = today.getLast(DayOfWeek.MONDAY)
14+
val lastTuesday: LocalDate get() = today.getLast(DayOfWeek.TUESDAY)
15+
val lastWednesday: LocalDate get() = today.getLast(DayOfWeek.WEDNESDAY)
16+
val lastThursday: LocalDate get() = today.getLast(DayOfWeek.THURSDAY)
17+
val lastFriday: LocalDate get() = today.getLast(DayOfWeek.FRIDAY)
18+
val lastSaturday: LocalDate get() = today.getLast(DayOfWeek.SATURDAY)
19+
val lastSunday: LocalDate get() = today.getLast(DayOfWeek.SUNDAY)
2020

21-
val nextMonday: LocalDate get() = javatimefun.localdate.LocalDates.today.getNext(DayOfWeek.MONDAY)
22-
val nextTuesday: LocalDate get() = javatimefun.localdate.LocalDates.today.getNext(DayOfWeek.TUESDAY)
23-
val nextWednesday: LocalDate get() = javatimefun.localdate.LocalDates.today.getNext(DayOfWeek.WEDNESDAY)
24-
val nextThursday: LocalDate get() = javatimefun.localdate.LocalDates.today.getNext(DayOfWeek.THURSDAY)
25-
val nextFriday: LocalDate get() = javatimefun.localdate.LocalDates.today.getNext(DayOfWeek.FRIDAY)
26-
val nextSaturday: LocalDate get() = javatimefun.localdate.LocalDates.today.getNext(DayOfWeek.SATURDAY)
27-
val nextSunday: LocalDate get() = javatimefun.localdate.LocalDates.today.getNext(DayOfWeek.SUNDAY)
21+
val nextMonday: LocalDate get() = today.getNext(DayOfWeek.MONDAY)
22+
val nextTuesday: LocalDate get() = today.getNext(DayOfWeek.TUESDAY)
23+
val nextWednesday: LocalDate get() = today.getNext(DayOfWeek.WEDNESDAY)
24+
val nextThursday: LocalDate get() = today.getNext(DayOfWeek.THURSDAY)
25+
val nextFriday: LocalDate get() = today.getNext(DayOfWeek.FRIDAY)
26+
val nextSaturday: LocalDate get() = today.getNext(DayOfWeek.SATURDAY)
27+
val nextSunday: LocalDate get() = today.getNext(DayOfWeek.SUNDAY)
2828

29-
val firstDayOfThisYear: LocalDate get() = javatimefun.localdate.LocalDateUtil.new(
30-
javatimefun.localdate.LocalDates.today.year,
31-
1,
32-
1
33-
)
34-
val lastDayOfThisYear: LocalDate get() = javatimefun.localdate.LocalDateUtil.new(
35-
javatimefun.localdate.LocalDates.today.year,
36-
12,
37-
31
38-
)
29+
val firstDayOfThisYear: LocalDate get() = LocalDateUtil.new(today.year, 1, 1)
30+
val lastDayOfThisYear: LocalDate get() = LocalDateUtil.new(today.year, 12, 31)
3931
}

src/main/kotlin/javatimefun/localdate/extensions/LocalDateComparisonExtensions.kt

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package javatimefun.localdate.extensions
22

33
import java.time.Duration
44
import java.time.LocalDate
5+
import java.time.temporal.ChronoUnit
56

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

77-
fun LocalDate.getMinuteDifference(localDateB: LocalDate): Long = Duration.between(this, localDateB).toMinutes()
78+
fun LocalDate.getSecondDifference(localDateB: LocalDate): Long =
79+
Duration.between(this.atStartOfDay(), localDateB.atStartOfDay()).toSeconds()
7880

79-
fun LocalDate.getHourDifference(localDateB: LocalDate): Long = Duration.between(this, localDateB).toHours()
81+
fun LocalDate.getMinuteDifference(localDateB: LocalDate): Long =
82+
Duration.between(this.atStartOfDay(), localDateB.atStartOfDay()).toMinutes()
8083

81-
fun LocalDate.getDayDifference(localDateB: LocalDate): Long = Duration.between(this.atStartOfDay(), localDateB.atStartOfDay()).toDays()
84+
fun LocalDate.getHourDifference(localDateB: LocalDate): Long =
85+
Duration.between(this.atStartOfDay(), localDateB.atStartOfDay()).toHours()
8286

83-
fun LocalDate.getMonthDifference(localDateB: LocalDate): Int {
84-
val yearDif = (localDateB.year - this.year) * 12
85-
return yearDif + (localDateB.month.value - this.month.value)
86-
}
87+
fun LocalDate.getDayDifference(localDateB: LocalDate): Long =
88+
ChronoUnit.DAYS.between(this, localDateB)
89+
90+
fun LocalDate.getMonthDifference(localDateB: LocalDate): Long =
91+
ChronoUnit.MONTHS.between(this, localDateB)
92+
93+
fun LocalDate.getYearDifference(localDateB: LocalDate): Long =
94+
ChronoUnit.YEARS.between(this, localDateB)

src/main/kotlin/javatimefun/localdatetime/LocalDateTimeUtil.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package javatimefun.localdatetime
22

3-
import javatimefun.date.extensions.toLocalDateTime
43
import java.time.Instant
54
import java.time.LocalDateTime
65
import java.time.Month
76
import java.time.ZoneOffset
8-
import java.util.Date
97

108
/**
119
* Contains helper functions that only serve to create new LocalDateTimes.

src/main/kotlin/javatimefun/localdatetime/extensions/LocalDateTimeComparisonExtensions.kt

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package javatimefun.localdatetime.extensions
22

33
import java.time.Duration
44
import java.time.LocalDateTime
5+
import java.time.temporal.ChronoUnit
56

67
// region Day Comparisons
78
fun LocalDateTime.compareDay(toDate: LocalDateTime): Int {
@@ -61,17 +62,23 @@ fun LocalDateTime.isAfterMonth(localDateTimeB: LocalDateTime): Boolean {
6162
// endregion
6263

6364
// region Year Comparisons
64-
fun LocalDateTime.compareYear(localDateTimeB: LocalDateTime): Int = this.year.compareTo(localDateTimeB.year)
65+
fun LocalDateTime.compareYear(localDateTimeB: LocalDateTime): Int =
66+
this.year.compareTo(localDateTimeB.year)
6567

66-
fun LocalDateTime.isBeforeYear(localDateTimeB: LocalDateTime): Boolean = this.year < localDateTimeB.year
68+
fun LocalDateTime.isBeforeYear(localDateTimeB: LocalDateTime): Boolean =
69+
this.year < localDateTimeB.year
6770

68-
fun LocalDateTime.isBeforeEqualYear(localDateTimeB: LocalDateTime): Boolean = this.year <= localDateTimeB.year
71+
fun LocalDateTime.isBeforeEqualYear(localDateTimeB: LocalDateTime): Boolean =
72+
this.year <= localDateTimeB.year
6973

70-
fun LocalDateTime.isEqualYear(localDateTimeB: LocalDateTime): Boolean = this.year == localDateTimeB.year
74+
fun LocalDateTime.isEqualYear(localDateTimeB: LocalDateTime): Boolean =
75+
this.year == localDateTimeB.year
7176

72-
fun LocalDateTime.isAfterEqualYear(localDateTimeB: LocalDateTime): Boolean = this.year >= localDateTimeB.year
77+
fun LocalDateTime.isAfterEqualYear(localDateTimeB: LocalDateTime): Boolean =
78+
this.year >= localDateTimeB.year
7379

74-
fun LocalDateTime.isAfterYear(localDateTimeB: LocalDateTime): Boolean = this.year > localDateTimeB.year
80+
fun LocalDateTime.isAfterYear(localDateTimeB: LocalDateTime): Boolean =
81+
this.year > localDateTimeB.year
7582
// endregion
7683

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

103+
fun LocalDateTime.getSecondDifference(localDateTimeB: LocalDateTime): Long =
104+
Duration.between(this, localDateTimeB).toSeconds()
105+
96106
fun LocalDateTime.getMinuteDifference(localDateTimeB: LocalDateTime): Long =
97107
Duration.between(this, localDateTimeB).toMinutes()
98108

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

105-
fun LocalDateTime.getMonthDifference(localDateTimeB: LocalDateTime): Int {
106-
val yearDif = (localDateTimeB.year - this.year) * 12
107-
return yearDif + (localDateTimeB.month.value - this.month.value)
108-
}
115+
fun LocalDateTime.getMonthDifference(localDateTimeB: LocalDateTime): Long =
116+
ChronoUnit.MONTHS.between(this, localDateTimeB)
117+
118+
fun LocalDateTime.getYearDifference(localDateTimeB: LocalDateTime): Long =
119+
ChronoUnit.YEARS.between(this, localDateTimeB)

src/main/kotlin/javatimefun/localtime/extensions/LocalTimeComparisonExtensions.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,14 @@ fun LocalTime.isAfterTime(b: LocalTime): Boolean = compareTime(b) > 0
2222
fun LocalTime.isAfterEqualTime(b: LocalTime): Boolean = compareTime(b) >= 0
2323
// endregion
2424

25-
fun LocalTime.getSecondDifference(localTimeB: LocalTime): Int = Duration.between(this, localTimeB).seconds.toInt()
25+
fun LocalTime.getMilliSecondDifference(localTimeB: LocalTime): Int =
26+
Duration.between(this, localTimeB).toMillis().toInt()
2627

27-
fun LocalTime.getMinuteDifference(localTimeB: LocalTime): Int = Duration.between(this, localTimeB).toMinutes().toInt()
28+
fun LocalTime.getSecondDifference(localTimeB: LocalTime): Int =
29+
Duration.between(this, localTimeB).seconds.toInt()
2830

29-
fun LocalTime.getHourDifference(localTimeB: LocalTime): Int = Duration.between(this, localTimeB).toHours().toInt()
31+
fun LocalTime.getMinuteDifference(localTimeB: LocalTime): Int =
32+
Duration.between(this, localTimeB).toMinutes().toInt()
33+
34+
fun LocalTime.getHourDifference(localTimeB: LocalTime): Int =
35+
Duration.between(this, localTimeB).toHours().toInt()

src/main/kotlin/javatimefun/zoneddatetime/ZonedDateTimeUtil.kt

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package javatimefun.zoneddatetime
22

3-
import java.time.*
3+
import java.time.Instant
4+
import java.time.LocalDateTime
5+
import java.time.Month
6+
import java.time.ZoneId
7+
import java.time.ZoneOffset
8+
import java.time.ZonedDateTime
49

510
/**
611
* Contains helper functions that only serve to create new ZonedDateTimes.
@@ -29,7 +34,8 @@ object ZonedDateTimeUtil {
2934
nano: Int = 0,
3035
useSystemTimeZone: Boolean = true
3136
): ZonedDateTime {
32-
val localDateTime = LocalDateTime.of(year, Month.of(month), day, hourIn24, minute, second, nano)
37+
val localDateTime =
38+
LocalDateTime.of(year, Month.of(month), day, hourIn24, minute, second, nano)
3339
return ZonedDateTime.of(
3440
localDateTime,
3541
if (useSystemTimeZone) ZoneId.systemDefault() else ZoneOffset.UTC
@@ -58,7 +64,16 @@ object ZonedDateTimeUtil {
5864
nano: Int = 0,
5965
isAm: Boolean,
6066
useSystemTimeZone: Boolean = true
61-
): ZonedDateTime = new(year, month, day, if (isAm) hour else hour + 12, minute, second, nano, useSystemTimeZone)
67+
): ZonedDateTime = new(
68+
year,
69+
month,
70+
day,
71+
if (isAm) hour else hour + 12,
72+
minute,
73+
second,
74+
nano,
75+
useSystemTimeZone
76+
)
6277

6378
/**
6479
* @param epochMilliseconds Epoch time, aka Unix time, are seconds elapsed since January 1st 1970 at 00:00:00 UTC.

src/main/kotlin/javatimefun/zoneddatetime/extensions/ZonedDateTimeComparisonExtensions.kt

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package javatimefun.zoneddatetime.extensions
22

33
import java.time.Duration
44
import java.time.ZonedDateTime
5+
import java.time.temporal.ChronoUnit
56

67
// region Year Comparisons
78
/**
@@ -155,35 +156,40 @@ fun ZonedDateTime.compareDay(zonedDateTimeB: ZonedDateTime): Int {
155156
* @param zonedDateTimeB ZonedDateTime of which we want to compare context's day to.
156157
* @return True if context's equals param's day, false otherwise.
157158
*/
158-
fun ZonedDateTime.isEqualDay(zonedDateTimeB: ZonedDateTime): Boolean = this.compareDay(zonedDateTimeB) == 0
159+
fun ZonedDateTime.isEqualDay(zonedDateTimeB: ZonedDateTime): Boolean =
160+
this.compareDay(zonedDateTimeB) == 0
159161

160162
/**
161163
* Works off of ZonedDateTime context.
162164
* @param zonedDateTimeB ZonedDateTime of which we want to compare context's day to.
163165
* @return True if context is before param's day, false otherwise.
164166
*/
165-
fun ZonedDateTime.isBeforeDay(zonedDateTimeB: ZonedDateTime): Boolean = this.compareDay(zonedDateTimeB) < 0
167+
fun ZonedDateTime.isBeforeDay(zonedDateTimeB: ZonedDateTime): Boolean =
168+
this.compareDay(zonedDateTimeB) < 0
166169

167170
/**
168171
* Works off of ZonedDateTime context.
169172
* @param zonedDateTimeB ZonedDateTime of which we want to compare context's day to.
170173
* @return True if context is before or equal param's day, false otherwise.
171174
*/
172-
fun ZonedDateTime.isBeforeEqualDay(zonedDateTimeB: ZonedDateTime): Boolean = this.compareDay(zonedDateTimeB) <= 0
175+
fun ZonedDateTime.isBeforeEqualDay(zonedDateTimeB: ZonedDateTime): Boolean =
176+
this.compareDay(zonedDateTimeB) <= 0
173177

174178
/**
175179
* Works off of ZonedDateTime context.
176180
* @param zonedDateTimeB ZonedDateTime of which we want to compare context's day to.
177181
* @return True if context is after param's day, false otherwise.
178182
*/
179-
fun ZonedDateTime.isAfterDay(zonedDateTimeB: ZonedDateTime): Boolean = this.compareDay(zonedDateTimeB) > 0
183+
fun ZonedDateTime.isAfterDay(zonedDateTimeB: ZonedDateTime): Boolean =
184+
this.compareDay(zonedDateTimeB) > 0
180185

181186
/**
182187
* Works off of ZonedDateTime context.
183188
* @param zonedDateTimeB ZonedDateTime of which we want to compare context's day to.
184189
* @return True if context is after or equal param's day, false otherwise.
185190
*/
186-
fun ZonedDateTime.isAfterEqualDay(zonedDateTimeB: ZonedDateTime): Boolean = this.compareDay(zonedDateTimeB) >= 0
191+
fun ZonedDateTime.isAfterEqualDay(zonedDateTimeB: ZonedDateTime): Boolean =
192+
this.compareDay(zonedDateTimeB) >= 0
187193
// endregion
188194

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

216223
/**
217224
* Works off of ZonedDateTime context.
218225
* @param zonedDateTimeB ZonedDateTime of which we want to compare context's day & time to.
219226
* @return True if context is before or equal param's day & time, false otherwise.
220227
*/
221-
fun ZonedDateTime.isBeforeEqualTime(zonedDateTimeB: ZonedDateTime): Boolean = this.compareTime(zonedDateTimeB) <= 0
228+
fun ZonedDateTime.isBeforeEqualTime(zonedDateTimeB: ZonedDateTime): Boolean =
229+
this.compareTime(zonedDateTimeB) <= 0
222230

223231
/**
224232
* Works off of ZonedDateTime context.
225233
* @param zonedDateTimeB ZonedDateTime of which we want to compare context's day & time to.
226234
* @return True if context is after param's day & time, false otherwise.
227235
*/
228-
fun ZonedDateTime.isAfterTime(zonedDateTimeB: ZonedDateTime): Boolean = this.compareTime(zonedDateTimeB) > 0
236+
fun ZonedDateTime.isAfterTime(zonedDateTimeB: ZonedDateTime): Boolean =
237+
this.compareTime(zonedDateTimeB) > 0
229238

230239
/**
231240
* Works off of ZonedDateTime context.
232241
* @param zonedDateTimeB ZonedDateTime of which we want to compare context's day & time to.
233242
* @return True if context is after or equal param's day & time, false otherwise.
234243
*/
235-
fun ZonedDateTime.isAfterEqualTime(zonedDateTimeB: ZonedDateTime): Boolean = this.compareTime(zonedDateTimeB) >= 0
244+
fun ZonedDateTime.isAfterEqualTime(zonedDateTimeB: ZonedDateTime): Boolean =
245+
this.compareTime(zonedDateTimeB) >= 0
236246
// endregion
237247

248+
/**
249+
* Works off of ZonedDateTime context.
250+
* @param zonedDateTimeB ZonedDateTime of which we want to compare context's second difference from.
251+
* @return Seconds away from param be it positive or negative.
252+
*/
253+
fun ZonedDateTime.getSecondDifference(zonedDateTimeB: ZonedDateTime): Long {
254+
val otherZonedDateTime = zonedDateTimeB.withTimeZoneOf(this)
255+
return Duration.between(this, otherZonedDateTime).toSeconds()
256+
}
257+
238258
/**
239259
* Works off of ZonedDateTime context.
240260
* @param zonedDateTimeB ZonedDateTime of which we want to compare context's minute difference from.
@@ -270,8 +290,17 @@ fun ZonedDateTime.getDayDifference(zonedDateTimeB: ZonedDateTime): Long {
270290
* @param zonedDateTimeB ZonedDateTime of which we want to compare context's months difference from.
271291
* @return Months away from param be it positive or negative.
272292
*/
273-
fun ZonedDateTime.getMonthDifference(zonedDateTimeB: ZonedDateTime): Int {
293+
fun ZonedDateTime.getMonthDifference(zonedDateTimeB: ZonedDateTime): Long {
294+
val otherZonedDateTime = zonedDateTimeB.withTimeZoneOf(this)
295+
return ChronoUnit.MONTHS.between(this, otherZonedDateTime)
296+
}
297+
298+
/**
299+
* Works off of ZonedDateTime context.
300+
* @param zonedDateTimeB ZonedDateTime of which we want to compare context's years difference from.
301+
* @return Years away from param be it positive or negative.
302+
*/
303+
fun ZonedDateTime.getYearDifference(zonedDateTimeB: ZonedDateTime): Long {
274304
val otherZonedDateTime = zonedDateTimeB.withTimeZoneOf(this)
275-
val yearDif = (otherZonedDateTime.year - this.year) * 12
276-
return yearDif + (otherZonedDateTime.month.value - this.month.value)
305+
return ChronoUnit.YEARS.between(this, otherZonedDateTime)
277306
}

0 commit comments

Comments
 (0)