Class DateTimeUtils
java.lang.Object
com.thanlinardos.spring_enterprise_library.time.utils.DateTimeUtils
Utility class for working with
LocalDateTime objects.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic LocalDateTimeaddDefault(LocalDateTime dateTime, long amount) Adds the given amount of the accuracy configured inTimeFactory.getAccuracy()to the given dateTime.static LocalDateTimeaddSingle(LocalDateTime dateTime) Adds a single unit of the accuracy configured inTimeFactory.getAccuracy()to the given dateTime.static LocalDateTimefromEndOfLocalDate(LocalDate date) Converts aLocalDateto aLocalDateTimeat the end of the day (23:59:59.999...).static LocalDateTimefromEndOfLocalDate(LocalDate date, TimeUnit accuracy) Converts aLocalDateto aLocalDateTimeat the end of the day (23:59:59.999...).static LocalDateTimefromLocalDate(LocalDate date) Converts aLocalDateto aLocalDateTimeat the start of the day (00:00).static booleanisAfterNullAsMax(LocalDateTime dateTime1, LocalDateTime dateTime2) Checks if dateTime1 is after dateTime2, treating null asTimeFactory.getMaxDate().static booleanisAfterOrEqual(LocalDateTime dateTime1, LocalDateTime dateTime2) Checks if dateTime1 is after or equal to dateTime2, treating null asTimeFactory.getMaxDate().static booleanisBeforeNullAsMin(LocalDateTime dateTime1, LocalDateTime dateTime2) Checks if dateTime1 is before dateTime2, treating null asTimeFactory.getMinDate().static booleanisBeforeOrEqual(LocalDateTime dateTime1, LocalDateTime dateTime2) Checks if dateTime1 is before or equal to dateTime2, treating null asTimeFactory.getMinDate().static LocalDateTimemaxNullAsMax(LocalDateTime dateTime1, LocalDateTime dateTime2) Returns the maximum of two dateTimes, treating null asTimeFactory.getMaxDateTime().static LocalDateTimemaxNullAsMin(LocalDateTime dateTime1, LocalDateTime dateTime2) Returns the maximum of two dateTimes, treating null asTimeFactory.getMinDateTime().static LocalDateTimeminNullAsMax(LocalDateTime dateTime1, LocalDateTime dateTime2) Returns the minimum of two dateTimes, treating null asTimeFactory.getMaxDateTime().static LocalDateTimeminNullAsMin(LocalDateTime dateTime1, LocalDateTime dateTime2) Returns the minimum of two dateTimes, treating null asTimeFactory.getMinDateTime().static LocalDateTimeparseDateTime(String start) Parses a dateTime string using the default parser inLocalDateTime.parse(CharSequence).static LocalDateTimeparseDateTime(String date, Function<String, LocalDateTime> parser) Parses an dateTime string using the provided parser function.static LocalDateTimesubtractDefault(LocalDateTime dateTime, long amount) Subtracts the given amount of the accuracy configured inTimeFactory.getAccuracy()from the given dateTime.static LocalDateTimesubtractDefault(LocalDateTime dateTime, long amount, TimeUnit accuracy) Subtracts the given amount of the specified accuracy from the given dateTime.static LocalDateTimesubtractSingle(LocalDateTime dateTime) Subtracts a single unit of the accuracy configured inTimeFactory.getAccuracy()from the given dateTime.static LocalDateTimesubtractSingle(LocalDateTime dateTime, TimeUnit accuracy) Subtracts a single unit of the specified accuracy from the given dateTime.static LocalDatetoLocalDate(LocalDateTime dateTime) Converts aLocalDateTimeto aLocalDatewith the default time zone.
-
Constructor Details
-
DateTimeUtils
public DateTimeUtils()
-
-
Method Details
-
parseDateTime
Parses a dateTime string using the default parser inLocalDateTime.parse(CharSequence).- Parameters:
start- the dateTime string to parse.- Returns:
- the parsed dateTime, or null if the input string is empty.
-
parseDateTime
public static LocalDateTime parseDateTime(@Nullable String date, Function<String, LocalDateTime> parser) Parses an dateTime string using the provided parser function.- Parameters:
date- the dateTime string to parse.parser- the function to parse the dateTime.- Returns:
- the parsed dateTime, or null if the input string is empty.
-
fromLocalDate
Converts aLocalDateto aLocalDateTimeat the start of the day (00:00).- Parameters:
date- the date to convert.- Returns:
- the converted dateTime, or null if the input date is null.
-
fromEndOfLocalDate
Converts aLocalDateto aLocalDateTimeat the end of the day (23:59:59.999...). This is achieved by converting the date to the start of the next day and then subtracting a single unit of the default accuracy.For example, if the default accuracy is set to MILLISECONDS, the resulting time will be 23:59:59.999.
- Parameters:
date- the date to convert.- Returns:
- the converted dateTime, or null if the input date is null.
-
fromEndOfLocalDate
@Nullable public static LocalDateTime fromEndOfLocalDate(@Nullable LocalDate date, TimeUnit accuracy) Converts aLocalDateto aLocalDateTimeat the end of the day (23:59:59.999...). SeefromEndOfLocalDate(LocalDate)for details.- Parameters:
date- the date to convert.accuracy- the accuracy to use for subtraction, overriding the default.- Returns:
- the converted dateTime, or null if the input date is null.
-
toLocalDate
Converts aLocalDateTimeto aLocalDatewith the default time zone.- Parameters:
dateTime- the dateTime to convert.- Returns:
- the converted date, or null if the input dateTime is null.
-
isBeforeNullAsMin
public static boolean isBeforeNullAsMin(@Nullable LocalDateTime dateTime1, @Nullable LocalDateTime dateTime2) Checks if dateTime1 is before dateTime2, treating null asTimeFactory.getMinDate().- Parameters:
dateTime1- The first date to compare.dateTime2- The second date to compare.- Returns:
- true if dateTime1 is before dateTime2, false otherwise.
-
isAfterNullAsMax
public static boolean isAfterNullAsMax(@Nullable LocalDateTime dateTime1, @Nullable LocalDateTime dateTime2) Checks if dateTime1 is after dateTime2, treating null asTimeFactory.getMaxDate().- Parameters:
dateTime1- The first date to compare.dateTime2- The second date to compare.- Returns:
- true if dateTime1 is after dateTime2, false otherwise.
-
isBeforeOrEqual
public static boolean isBeforeOrEqual(@Nullable LocalDateTime dateTime1, @Nullable LocalDateTime dateTime2) Checks if dateTime1 is before or equal to dateTime2, treating null asTimeFactory.getMinDate().- Parameters:
dateTime1- The first date to compare.dateTime2- The second date to compare.- Returns:
- true if dateTime1 is before or equal to dateTime2, false otherwise.
-
isAfterOrEqual
public static boolean isAfterOrEqual(@Nullable LocalDateTime dateTime1, @Nullable LocalDateTime dateTime2) Checks if dateTime1 is after or equal to dateTime2, treating null asTimeFactory.getMaxDate().- Parameters:
dateTime1- The first date to compare.dateTime2- The second date to compare.- Returns:
- true if dateTime1 is after or equal to dateTime2, false otherwise.
-
maxNullAsMax
public static LocalDateTime maxNullAsMax(@Nullable LocalDateTime dateTime1, @Nullable LocalDateTime dateTime2) Returns the maximum of two dateTimes, treating null asTimeFactory.getMaxDateTime().- Parameters:
dateTime1- the first dateTime.dateTime2- the second dateTime.- Returns:
- the maximum of the two dateTimes.
-
minNullAsMin
public static LocalDateTime minNullAsMin(@Nullable LocalDateTime dateTime1, @Nullable LocalDateTime dateTime2) Returns the minimum of two dateTimes, treating null asTimeFactory.getMinDateTime().- Parameters:
dateTime1- the first dateTime.dateTime2- the second dateTime.- Returns:
- the minimum of the two dateTimes.
-
maxNullAsMin
public static LocalDateTime maxNullAsMin(@Nullable LocalDateTime dateTime1, @Nullable LocalDateTime dateTime2) Returns the maximum of two dateTimes, treating null asTimeFactory.getMinDateTime().- Parameters:
dateTime1- the first dateTime.dateTime2- the second dateTime.- Returns:
- the maximum of the two dateTimes.
-
minNullAsMax
public static LocalDateTime minNullAsMax(@Nullable LocalDateTime dateTime1, @Nullable LocalDateTime dateTime2) Returns the minimum of two dateTimes, treating null asTimeFactory.getMaxDateTime().- Parameters:
dateTime1- the first dateTime.dateTime2- the second dateTime.- Returns:
- the minimum of the two dateTimes.
-
addDefault
Adds the given amount of the accuracy configured inTimeFactory.getAccuracy()to the given dateTime.For example if the default accuracy is MILLISECONDS, and the amount is 5, then 5 millis will be added to the given dateTime.
- Parameters:
dateTime- the dateTime to add to.amount- the amount to add.- Returns:
- the adjusted dateTime, or null if the given dateTime is null.
-
addSingle
Adds a single unit of the accuracy configured inTimeFactory.getAccuracy()to the given dateTime.- Parameters:
dateTime- the dateTime to add to.- Returns:
- the adjusted dateTime, or null if the given dateTime is null.
-
subtractDefault
@Nullable public static LocalDateTime subtractDefault(@Nullable LocalDateTime dateTime, long amount) Subtracts the given amount of the accuracy configured inTimeFactory.getAccuracy()from the given dateTime.For example if the default accuracy is MILLISECONDS, and the amount is 5, then 5 millis will be subtracted from the given dateTime.
- Parameters:
dateTime- the dateTime to subtract from.amount- the amount to subtract.- Returns:
- the adjusted dateTime, or null if the given dateTime is null.
-
subtractSingle
Subtracts a single unit of the accuracy configured inTimeFactory.getAccuracy()from the given dateTime.- Parameters:
dateTime- the dateTime to subtract from.- Returns:
- the adjusted dateTime, or null if the given dateTime is null.
-
subtractDefault
@Nullable public static LocalDateTime subtractDefault(@Nullable LocalDateTime dateTime, long amount, TimeUnit accuracy) Subtracts the given amount of the specified accuracy from the given dateTime.- Parameters:
dateTime- the dateTime to subtract from.amount- the amount to subtract.accuracy- the accuracy to use for subtraction, overriding the default.- Returns:
- the adjusted dateTime, or null if the given dateTime is null.
-
subtractSingle
@Nullable public static LocalDateTime subtractSingle(@Nullable LocalDateTime dateTime, TimeUnit accuracy) Subtracts a single unit of the specified accuracy from the given dateTime.- Parameters:
dateTime- the dateTime to subtract from.accuracy- the accuracy to use for subtraction, overriding the default.- Returns:
- the adjusted dateTime, or null if the given dateTime is null.
-