-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #496 from scireum/feature/mko/SIRI-885
Adds a new helper for dates (called Dates) which provides convenience methods
- Loading branch information
Showing
2 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |