diff --git a/src/main/java/sirius/kernel/commons/Dates.java b/src/main/java/sirius/kernel/commons/Dates.java new file mode 100644 index 00000000..306d13b0 --- /dev/null +++ b/src/main/java/sirius/kernel/commons/Dates.java @@ -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 null 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 null if no date is given + */ + public static LocalDateTime computeLatestDateTime(List 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 null 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 null if no date is given + */ + public static LocalDate computeLatestDate(List 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 null 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 null if no date is given + */ + public static LocalDateTime computeEarliestDateTime(List 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 null 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 null if no date is given + */ + public static LocalDate computeEarliestDate(List dates) { + return dates.stream() + .filter(Objects::nonNull) + .min(LocalDate::compareTo) + .orElse(null); + } +} diff --git a/src/test/kotlin/sirius/kernel/commons/DatesTest.kt b/src/test/kotlin/sirius/kernel/commons/DatesTest.kt new file mode 100644 index 00000000..4851e2a7 --- /dev/null +++ b/src/test/kotlin/sirius/kernel/commons/DatesTest.kt @@ -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)) + } +}