Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds date comparison methods #533

Merged
merged 2 commits into from
Aug 9, 2024
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
58 changes: 42 additions & 16 deletions src/main/java/sirius/kernel/commons/Dates.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

package sirius.kernel.commons;

import javax.annotation.Nonnull;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
Expand Down Expand Up @@ -42,10 +43,7 @@ public static LocalDateTime computeLatestDateTime(LocalDateTime... dateTimes) {
* @return the latest date-time object or <tt>null</tt> if no date is given
*/
public static LocalDateTime computeLatestDateTime(List<LocalDateTime> dateTimes) {
return dateTimes.stream()
.filter(Objects::nonNull)
.max(LocalDateTime::compareTo)
.orElse(null);
return dateTimes.stream().filter(Objects::nonNull).max(LocalDateTime::compareTo).orElse(null);
}

/**
Expand All @@ -60,14 +58,12 @@ public static LocalDate computeLatestDate(LocalDate... dates) {

/**
* Computes the latest date of the given date objects.
*
* @param dates a list of date objects to compare
* @return the latest date object or <tt>null</tt> if no date is given
*/
public static LocalDate computeLatestDate(List<LocalDate> dates) {
return dates.stream()
.filter(Objects::nonNull)
.max(LocalDate::compareTo)
.orElse(null);
return dates.stream().filter(Objects::nonNull).max(LocalDate::compareTo).orElse(null);
}

/**
Expand All @@ -87,10 +83,7 @@ public static LocalDateTime computeEarliestDateTime(LocalDateTime... dateTimes)
* @return the earliest date-time object or <tt>null</tt> if no date is given
*/
public static LocalDateTime computeEarliestDateTime(List<LocalDateTime> dateTimes) {
return dateTimes.stream()
.filter(Objects::nonNull)
.min(LocalDateTime::compareTo)
.orElse(null);
return dateTimes.stream().filter(Objects::nonNull).min(LocalDateTime::compareTo).orElse(null);
}

/**
Expand All @@ -110,9 +103,42 @@ public static LocalDate computeEarliestDate(LocalDate... dates) {
* @return the earliest date object or <tt>null</tt> if no date is given
*/
public static LocalDate computeEarliestDate(List<LocalDate> dates) {
return dates.stream()
.filter(Objects::nonNull)
.min(LocalDate::compareTo)
.orElse(null);
return dates.stream().filter(Objects::nonNull).min(LocalDate::compareTo).orElse(null);
}

/**
* Determines if the given date is before or after the given reference date.
*
* @param dateToCheck the date to check
* @param referenceDate the reference date
* @return <tt>true</tt> if the date is before or after the reference date, <tt>false</tt> otherwise
*/
public static boolean isBeforeOrEqual(@Nonnull LocalDate dateToCheck, @Nonnull LocalDate referenceDate) {
return dateToCheck.isBefore(referenceDate) || dateToCheck.equals(referenceDate);
}

/**
* Determines if the given date is after or equal to the given reference date.
*
* @param dateToCheck the date to check
* @param referenceDate the reference date
* @return <tt>true</tt> if the date is after or equal to the reference date, <tt>false</tt> otherwise
*/
public static boolean isAfterOrEqual(@Nonnull LocalDate dateToCheck, @Nonnull LocalDate referenceDate) {
return dateToCheck.isAfter(referenceDate) || dateToCheck.equals(referenceDate);
}

/**
* Determines if the given date is within the given range, inclusive the range dates.
*
* @param startDate the range's start date
* @param endDate the range's end date
* @param dateToCheck the date to check
* @return <tt>true</tt> if the date is within the range, <tt>false</tt> otherwise
*/
public static boolean isWithinRange(@Nonnull LocalDate startDate,
@Nonnull LocalDate endDate,
@Nonnull LocalDate dateToCheck) {
return isAfterOrEqual(dateToCheck, startDate) && isBeforeOrEqual(dateToCheck, endDate);
}
}
36 changes: 35 additions & 1 deletion src/test/kotlin/sirius/kernel/commons/DatesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ package sirius.kernel.commons

import java.time.LocalDate
import java.time.LocalDateTime
import kotlin.test.*
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue

/**
* Tests the [Dates] class.
Expand Down Expand Up @@ -51,4 +54,35 @@ class DatesTest {
assertEquals(theDayBeforeYesterday, Dates.computeEarliestDate(null, theDayBeforeYesterday, now, yesterday))
assertEquals(null, Dates.computeEarliestDate(null, null, null))
}

@Test
fun isBeforeOrEqual() {
val theDayBeforeYesterday = LocalDate.now().minusDays(2)
val yesterday = LocalDate.now().minusDays(1)
val now = LocalDate.now()
assertTrue(Dates.isBeforeOrEqual(theDayBeforeYesterday, yesterday))
assertTrue(Dates.isBeforeOrEqual(yesterday, yesterday))
assertFalse(Dates.isBeforeOrEqual(now, yesterday))
}

@Test
fun isAfterOrEqual() {
val theDayBeforeYesterday = LocalDate.now().minusDays(2)
val yesterday = LocalDate.now().minusDays(1)
val now = LocalDate.now()
assertTrue(Dates.isAfterOrEqual(now, yesterday))
assertTrue(Dates.isAfterOrEqual(yesterday, yesterday))
assertFalse(Dates.isAfterOrEqual(theDayBeforeYesterday, yesterday))
}

@Test
fun isWithinRange() {
val theDayBeforeYesterday = LocalDate.now().minusDays(2)
val yesterday = LocalDate.now().minusDays(1)
val now = LocalDate.now()
assertTrue(Dates.isWithinRange(theDayBeforeYesterday, now, yesterday))
assertTrue(Dates.isWithinRange(theDayBeforeYesterday, now, theDayBeforeYesterday))
assertTrue(Dates.isWithinRange(theDayBeforeYesterday, now, now))
assertFalse(Dates.isWithinRange(theDayBeforeYesterday, yesterday, now))
}
}