@@ -2,6 +2,7 @@ package javatimefun.zoneddatetime.extensions
2
2
3
3
import java.time.Duration
4
4
import java.time.ZonedDateTime
5
+ import java.time.temporal.ChronoUnit
5
6
6
7
// region Year Comparisons
7
8
/* *
@@ -155,35 +156,40 @@ fun ZonedDateTime.compareDay(zonedDateTimeB: ZonedDateTime): Int {
155
156
* @param zonedDateTimeB ZonedDateTime of which we want to compare context's day to.
156
157
* @return True if context's equals param's day, false otherwise.
157
158
*/
158
- fun ZonedDateTime.isEqualDay (zonedDateTimeB : ZonedDateTime ): Boolean = this .compareDay(zonedDateTimeB) == 0
159
+ fun ZonedDateTime.isEqualDay (zonedDateTimeB : ZonedDateTime ): Boolean =
160
+ this .compareDay(zonedDateTimeB) == 0
159
161
160
162
/* *
161
163
* Works off of ZonedDateTime context.
162
164
* @param zonedDateTimeB ZonedDateTime of which we want to compare context's day to.
163
165
* @return True if context is before param's day, false otherwise.
164
166
*/
165
- fun ZonedDateTime.isBeforeDay (zonedDateTimeB : ZonedDateTime ): Boolean = this .compareDay(zonedDateTimeB) < 0
167
+ fun ZonedDateTime.isBeforeDay (zonedDateTimeB : ZonedDateTime ): Boolean =
168
+ this .compareDay(zonedDateTimeB) < 0
166
169
167
170
/* *
168
171
* Works off of ZonedDateTime context.
169
172
* @param zonedDateTimeB ZonedDateTime of which we want to compare context's day to.
170
173
* @return True if context is before or equal param's day, false otherwise.
171
174
*/
172
- fun ZonedDateTime.isBeforeEqualDay (zonedDateTimeB : ZonedDateTime ): Boolean = this .compareDay(zonedDateTimeB) <= 0
175
+ fun ZonedDateTime.isBeforeEqualDay (zonedDateTimeB : ZonedDateTime ): Boolean =
176
+ this .compareDay(zonedDateTimeB) <= 0
173
177
174
178
/* *
175
179
* Works off of ZonedDateTime context.
176
180
* @param zonedDateTimeB ZonedDateTime of which we want to compare context's day to.
177
181
* @return True if context is after param's day, false otherwise.
178
182
*/
179
- fun ZonedDateTime.isAfterDay (zonedDateTimeB : ZonedDateTime ): Boolean = this .compareDay(zonedDateTimeB) > 0
183
+ fun ZonedDateTime.isAfterDay (zonedDateTimeB : ZonedDateTime ): Boolean =
184
+ this .compareDay(zonedDateTimeB) > 0
180
185
181
186
/* *
182
187
* Works off of ZonedDateTime context.
183
188
* @param zonedDateTimeB ZonedDateTime of which we want to compare context's day to.
184
189
* @return True if context is after or equal param's day, false otherwise.
185
190
*/
186
- fun ZonedDateTime.isAfterEqualDay (zonedDateTimeB : ZonedDateTime ): Boolean = this .compareDay(zonedDateTimeB) >= 0
191
+ fun ZonedDateTime.isAfterEqualDay (zonedDateTimeB : ZonedDateTime ): Boolean =
192
+ this .compareDay(zonedDateTimeB) >= 0
187
193
// endregion
188
194
189
195
// region Time Comparisons
@@ -211,30 +217,44 @@ fun ZonedDateTime.isEqualTime(zonedDateTimeB: ZonedDateTime): Boolean = this.isE
211
217
* @param zonedDateTimeB ZonedDateTime of which we want to compare context's day & time to.
212
218
* @return True if context is before param's day & time, false otherwise.
213
219
*/
214
- fun ZonedDateTime.isBeforeTime (zonedDateTimeB : ZonedDateTime ): Boolean = this .isBefore(zonedDateTimeB)
220
+ fun ZonedDateTime.isBeforeTime (zonedDateTimeB : ZonedDateTime ): Boolean =
221
+ this .isBefore(zonedDateTimeB)
215
222
216
223
/* *
217
224
* Works off of ZonedDateTime context.
218
225
* @param zonedDateTimeB ZonedDateTime of which we want to compare context's day & time to.
219
226
* @return True if context is before or equal param's day & time, false otherwise.
220
227
*/
221
- fun ZonedDateTime.isBeforeEqualTime (zonedDateTimeB : ZonedDateTime ): Boolean = this .compareTime(zonedDateTimeB) <= 0
228
+ fun ZonedDateTime.isBeforeEqualTime (zonedDateTimeB : ZonedDateTime ): Boolean =
229
+ this .compareTime(zonedDateTimeB) <= 0
222
230
223
231
/* *
224
232
* Works off of ZonedDateTime context.
225
233
* @param zonedDateTimeB ZonedDateTime of which we want to compare context's day & time to.
226
234
* @return True if context is after param's day & time, false otherwise.
227
235
*/
228
- fun ZonedDateTime.isAfterTime (zonedDateTimeB : ZonedDateTime ): Boolean = this .compareTime(zonedDateTimeB) > 0
236
+ fun ZonedDateTime.isAfterTime (zonedDateTimeB : ZonedDateTime ): Boolean =
237
+ this .compareTime(zonedDateTimeB) > 0
229
238
230
239
/* *
231
240
* Works off of ZonedDateTime context.
232
241
* @param zonedDateTimeB ZonedDateTime of which we want to compare context's day & time to.
233
242
* @return True if context is after or equal param's day & time, false otherwise.
234
243
*/
235
- fun ZonedDateTime.isAfterEqualTime (zonedDateTimeB : ZonedDateTime ): Boolean = this .compareTime(zonedDateTimeB) >= 0
244
+ fun ZonedDateTime.isAfterEqualTime (zonedDateTimeB : ZonedDateTime ): Boolean =
245
+ this .compareTime(zonedDateTimeB) >= 0
236
246
// endregion
237
247
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
+
238
258
/* *
239
259
* Works off of ZonedDateTime context.
240
260
* @param zonedDateTimeB ZonedDateTime of which we want to compare context's minute difference from.
@@ -270,8 +290,17 @@ fun ZonedDateTime.getDayDifference(zonedDateTimeB: ZonedDateTime): Long {
270
290
* @param zonedDateTimeB ZonedDateTime of which we want to compare context's months difference from.
271
291
* @return Months away from param be it positive or negative.
272
292
*/
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 {
274
304
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)
277
306
}
0 commit comments