Package dev.nm.misc.datastructure.time
Class LocalDateTimeUtils
- java.lang.Object
-
- dev.nm.misc.datastructure.time.LocalDateTimeUtils
-
public class LocalDateTimeUtils extends Object
Utility functions to manipulate LocalDateTime.
-
-
Constructor Summary
Constructors Constructor Description LocalDateTimeUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static int
comparePeriods(Period period1, Period period2)
Compares two periods, assuming 30 days per month.static int
comparePeriods(Period period1, Period period2, int nDaysPerMonth)
Compares two periods, with the assumed number of days per month.static List<LocalDateTimeInterval>
fixedSizeIntervals(LocalDateTime start, LocalDateTime end, Period period)
Creates a list of intervals betweenstart
andend
every so often.static boolean
isWeekend(LocalDateTime time)
Checks if the given time is a weekend.static LocalDateTime
max(LocalDateTime t1, LocalDateTime t2)
Returns the later of the two given time instances, or the first instance if two instances are equal.static LocalDateTime
min(LocalDateTime t1, LocalDateTime t2)
Returns the earlier of the two given time instances, or the first instance if two instances are equal.static LocalDateTime
nextWeekDay(LocalDateTime time)
Gets the next weekday, i.e., skipping Saturdays and Sundays.static long
periodCountBetween(LocalDateTime t1, LocalDateTime t2, Period period)
Returns the number of whole periods between two time instants.static long
periodDayCount(Period period, int nDaysPerMonth)
Estimates the number of days in a period, with the assumed number of days per month.static List<LocalDateTime>
periodicInstants(LocalDateTime startTime, Period period, int nPeriods)
Generates a list of periodic time instants.static List<LocalDateTime>
periodicInstantsSpanningInterval(LocalDateTimeInterval interval, Period period)
Generates a list of periodic time instants which span the whole given interval.static List<LocalDateTime>
periodicInstantsWithinInterval(LocalDateTimeInterval interval, Period period)
Generates a list of periodic time instants within a given interval.static LocalDateTime
previousWeekDay(LocalDateTime time)
Gets the previous weekday, i.e., skipping Saturdays and Sundays.
-
-
-
Method Detail
-
min
public static LocalDateTime min(LocalDateTime t1, LocalDateTime t2)
Returns the earlier of the two given time instances, or the first instance if two instances are equal.- Parameters:
t1
- the first time instancet2
- the second time instance- Returns:
- the earlier time instance
-
max
public static LocalDateTime max(LocalDateTime t1, LocalDateTime t2)
Returns the later of the two given time instances, or the first instance if two instances are equal.- Parameters:
t1
- the first time instancet2
- the second time instance- Returns:
- the later time instance
-
isWeekend
public static boolean isWeekend(LocalDateTime time)
Checks if the given time is a weekend.- Parameters:
time
- a time- Returns:
true
if the given time is a weekend
-
nextWeekDay
public static LocalDateTime nextWeekDay(LocalDateTime time)
Gets the next weekday, i.e., skipping Saturdays and Sundays.- Parameters:
time
- aLocalDateTime
- Returns:
- the next weekday
-
previousWeekDay
public static LocalDateTime previousWeekDay(LocalDateTime time)
Gets the previous weekday, i.e., skipping Saturdays and Sundays.- Parameters:
time
- aLocalDateTime
- Returns:
- the previous weekday
-
periodicInstants
public static List<LocalDateTime> periodicInstants(LocalDateTime startTime, Period period, int nPeriods)
Generates a list of periodic time instants. The number of instants equals to the number of periods plus one.- Parameters:
startTime
- the start timeperiod
- the periodnPeriods
- the number of periods- Returns:
- a list of periodic time instants
-
periodicInstantsWithinInterval
public static List<LocalDateTime> periodicInstantsWithinInterval(LocalDateTimeInterval interval, Period period)
Generates a list of periodic time instants within a given interval. That is, the first instant is the start of the interval, and the last instant is before the end of the interval.- Parameters:
interval
- the intervalperiod
- the period- Returns:
- a list of periodic time instants
-
periodicInstantsSpanningInterval
public static List<LocalDateTime> periodicInstantsSpanningInterval(LocalDateTimeInterval interval, Period period)
Generates a list of periodic time instants which span the whole given interval. That is, the first instant is the start of the interval, and the last instant is after the end of the interval.- Parameters:
interval
- the intervalperiod
- the period- Returns:
- a list of periodic time instants
-
periodCountBetween
public static long periodCountBetween(LocalDateTime t1, LocalDateTime t2, Period period)
Returns the number of whole periods between two time instants.- Parameters:
t1
- the first instantt2
- the second instantperiod
- a period- Returns:
- the number of periods between t1 and t2
-
comparePeriods
public static int comparePeriods(Period period1, Period period2)
Compares two periods, assuming 30 days per month.- Parameters:
period1
- the first periodperiod2
- the second period- Returns:
- 0 if two periods are of the same length; negative if the period is shorter; positive otherwise
-
comparePeriods
public static int comparePeriods(Period period1, Period period2, int nDaysPerMonth)
Compares two periods, with the assumed number of days per month.- Parameters:
period1
- the first periodperiod2
- the second periodnDaysPerMonth
- the assumed number of days per month- Returns:
- 0 if two periods are of the same length; negative if the period is shorter; positive otherwise
-
periodDayCount
public static long periodDayCount(Period period, int nDaysPerMonth)
Estimates the number of days in a period, with the assumed number of days per month.- Parameters:
period
- the periodnDaysPerMonth
- the assumed number of days per month- Returns:
- the estimated day count
-
fixedSizeIntervals
public static List<LocalDateTimeInterval> fixedSizeIntervals(LocalDateTime start, LocalDateTime end, Period period)
Creates a list of intervals betweenstart
andend
every so often. Note that the last interval ends at the given end time, so it can be shorter than the given period.- Parameters:
start
- the start timeend
- the end timeperiod
- the period or length of each interval- Returns:
- a list of intervals
-
-