Skip to content

Commit

Permalink
Merge pull request #496 from scireum/feature/mko/SIRI-885
Browse files Browse the repository at this point in the history
Adds a new helper for dates (called Dates) which provides convenience methods
  • Loading branch information
mko-sci authored Oct 4, 2023
2 parents 4792c98 + 5dc99cf commit 0f92dae
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 0 deletions.
118 changes: 118 additions & 0 deletions src/main/java/sirius/kernel/commons/Dates.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - info@scireum.de
*/

package sirius.kernel.commons;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
* Provides various helper methods for dealing with dates.
*/
public class Dates {

/**
* All methods are static, therefore no instances are required.
*/
private Dates() {
}

/**
* Computes the latest date-time of the given date-time objects.
*
* @param dateTimes the date-time objects to compare
* @return the latest date-time object or <tt>null</tt> if no date is given
*/
public static LocalDateTime computeLatestDateTime(LocalDateTime... dateTimes) {
return Dates.computeLatestDateTime(Arrays.asList(dateTimes));
}

/**
* Computes the latest date-time of the given date-time objects.
*
* @param dateTimes a list of date-time objects to compare
* @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);
}

/**
* Computes the latest date of the given date objects.
*
* @param dates the dates to compare
* @return the latest date object or <tt>null</tt> if no date is given
*/
public static LocalDate computeLatestDate(LocalDate... dates) {
return computeLatestDate(Arrays.asList(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);
}

/**
* Computes the earliest date-time of the given date-time objects.
*
* @param dateTimes the date-time objects to compare
* @return the earliest date-time object or <tt>null</tt> if no date is given
*/
public static LocalDateTime computeEarliestDateTime(LocalDateTime... dateTimes) {
return computeEarliestDateTime(Arrays.asList(dateTimes));
}

/**
* Computes the earliest date-time of the given date-time objects.
*
* @param dateTimes a list of date-time objects to compare
* @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);
}

/**
* Computes the earliest date of the given date objects.
*
* @param dates the dates to compare
* @return the earliest date object or <tt>null</tt> if no date is given
*/
public static LocalDate computeEarliestDate(LocalDate... dates) {
return computeEarliestDate(Arrays.asList(dates));
}

/**
* Computes the earliest date of the given date objects.
*
* @param dates a list of date objects to compare
* @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);
}
}
54 changes: 54 additions & 0 deletions src/test/kotlin/sirius/kernel/commons/DatesTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - info@scireum.de
*/

package sirius.kernel.commons

import java.time.LocalDate
import java.time.LocalDateTime
import kotlin.test.*

/**
* Tests the [Dates] class.
*/
class DatesTest {
@Test
fun computeLatestDateTime() {
val theDayBeforeYesterday = LocalDateTime.now().minusDays(2)
val yesterday = LocalDateTime.now().minusDays(1)
val now = LocalDateTime.now()
assertEquals(now, Dates.computeLatestDateTime(null, theDayBeforeYesterday, now, yesterday))
assertEquals(null, Dates.computeLatestDateTime(null, null, null))
}

@Test
fun computeLatestDate() {
val theDayBeforeYesterday = LocalDate.now().minusDays(2)
val yesterday = LocalDate.now().minusDays(1)
val now = LocalDate.now()
assertEquals(now, Dates.computeLatestDate(null, theDayBeforeYesterday, now, yesterday))
assertEquals(null, Dates.computeLatestDate(null, null, null))
}

@Test
fun computeEarliestDateTime() {
val theDayBeforeYesterday = LocalDateTime.now().minusDays(2)
val yesterday = LocalDateTime.now().minusDays(1)
val now = LocalDateTime.now()
assertEquals(theDayBeforeYesterday, Dates.computeEarliestDateTime(null, theDayBeforeYesterday, now, yesterday))
assertEquals(null, Dates.computeEarliestDateTime(null, null, null))
}

@Test
fun computeEarliestDate() {
val theDayBeforeYesterday = LocalDate.now().minusDays(2)
val yesterday = LocalDate.now().minusDays(1)
val now = LocalDate.now()
assertEquals(theDayBeforeYesterday, Dates.computeEarliestDate(null, theDayBeforeYesterday, now, yesterday))
assertEquals(null, Dates.computeEarliestDate(null, null, null))
}
}

0 comments on commit 0f92dae

Please sign in to comment.