Package com.fizzed.crux.util
Class JavaTimes
- java.lang.Object
-
- com.fizzed.crux.util.JavaTimes
-
public class JavaTimes extends java.lang.Object
-
-
Constructor Summary
Constructors Constructor Description JavaTimes()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static TimeDurationage(java.time.Instant instant)Calculates the age of a datetime from 'now'.static TimeDurationage(java.time.Instant instant, long epochMillis)Calculates the age of a datetime against a reference epoch millis.static TimeDurationage(java.time.ZonedDateTime dt)Calculates the age of a datetime from 'now'.static TimeDurationage(java.time.ZonedDateTime dt, long epochMillis)Calculates the age of a datetime against a reference epoch millis.static booleanequals(java.time.ZonedDateTime a, java.time.ZonedDateTime b)Tests equality (ignoring timezones).static booleangt(java.time.Instant a, java.time.Instant b)Isavalue greater thanb.static booleangte(java.time.Instant a, java.time.Instant b)Isavalue greater than or equal tob.static booleanlt(java.time.Instant a, java.time.Instant b)Isavalue less thanb.static booleanlte(java.time.Instant a, java.time.Instant b)Isavalue less than or equal tob.static java.time.Instantmax(java.time.Instant... dts)Returns the minimum of all instants.static java.time.Instantmax(java.time.Instant a, java.time.Instant b)Returns a if greater than or equal to b, otherwise b.static java.time.Instantmin(java.time.Instant... dts)Returns the minimum of all instants.static java.time.Instantmin(java.time.Instant a, java.time.Instant b)Returns a if less than or equal to b, otherwise b.static java.time.ZonedDateTimenow()Returns now() in UTC.static booleanoverlap(java.time.Instant start1, java.time.Instant end1, java.time.Instant start2, java.time.Instant end2)Is there an overlap in range1 (start1toend1) and range2 (start2toend2) null values are treated asInstant.MINfor start andInstant.MAXfor end.static booleanoverlap(java.time.Instant start1, java.time.Instant end1, java.time.Instant start2, java.time.Instant end2, boolean endInclusive)Is there an overlap in range1 (start1toend1) and range2 (start2toend2) null values are treated asInstant.MINfor start andInstant.MAXfor end.static booleanoverlap(java.time.Instant start1, java.time.Instant end1, java.time.Instant start2, java.time.Instant end2, boolean startInclusive, boolean endInclusive)Is there an overlap in range1 (start1toend1) and range2 (start2toend2) null values are treated asInstant.MINfor start andInstant.MAXfor end.static booleansameMillisPrecision(java.time.Instant a, java.time.Instant b, boolean rounding)Tests for similarity down to millisecond precision.static java.lang.Stringuptime(java.lang.Long startEpochMillis)static java.lang.Stringuptime(java.lang.Long endEpochMillis, java.lang.Long startEpochMillis)static java.lang.Stringuptime(java.time.Duration duration)static java.lang.Stringuptime(java.time.Instant start)Formats a string of "uptime" relative to NOW such as "6m 1s 467ms"static java.lang.Stringuptime(java.time.Instant end, java.time.Instant start)static java.lang.Stringuptime(java.time.ZonedDateTime start)static java.lang.Stringuptime(java.time.ZonedDateTime end, java.time.ZonedDateTime start)static booleanwithin(java.time.Instant dt, java.time.Instant start, java.time.Instant end)Whether instant is within start and end instants.static booleanwithin(java.time.Instant dt, java.time.Instant start, java.time.Instant end, boolean endInclusive)Whether instant is within start and end instants.The min of start and end is detected so the ordering does not matter.static booleanwithin(java.time.Instant dt, java.time.Instant start, java.time.Instant end, boolean startInclusive, boolean endInclusive)Whether instant is within start and end instants.The min of start and end is detected so the ordering does not matter.By default, this method makes the start date INCLUSIVE and the end date EXCLUSIVE/INCLUSIVE depending on what you pass in.static java.time.ZonedDateTimezonedDateTime(java.time.Instant instant)
-
-
-
Method Detail
-
zonedDateTime
public static java.time.ZonedDateTime zonedDateTime(java.time.Instant instant)
-
now
public static java.time.ZonedDateTime now()
Returns now() in UTC.- Returns:
- Now in UTC
-
equals
public static boolean equals(java.time.ZonedDateTime a, java.time.ZonedDateTime b)Tests equality (ignoring timezones).- Parameters:
a-b-- Returns:
-
sameMillisPrecision
public static boolean sameMillisPrecision(java.time.Instant a, java.time.Instant b, boolean rounding)Tests for similarity down to millisecond precision. Uses a truncating strategy for the "nanos" part of the value by default, unless rounding is true in which case the nanos may cause the millis to be also checked if they would round up.- Parameters:
a-b-rounding- If the nanos should be cause the milliseconds to be rounded up- Returns:
- True if same moment in time otherwise false
-
age
public static TimeDuration age(java.time.Instant instant)
Calculates the age of a datetime from 'now'. Just like your birthday, if the datetime is in the past then you a positive duration will be returned, if in the future then negative. For example, if the datetime is April 1 and 'now' is April 2, then the age will be a positive 1 day.- Parameters:
instant- The datetime to calculate the age of.- Returns:
-
age
public static TimeDuration age(java.time.ZonedDateTime dt)
Calculates the age of a datetime from 'now'. Just like your birthday, if the datetime is in the past then you a positive duration will be returned, if in the future then negative. For example, if the datetime is April 1 and 'now' is April 2, then the age will be a positive 1 day.- Parameters:
dt- The datetime to calculate the age of.- Returns:
-
age
public static TimeDuration age(java.time.Instant instant, long epochMillis)
Calculates the age of a datetime against a reference epoch millis. If the datetime is on April 1 and the epochMillis is on April 2, then the age will be a positive 1 day.- Parameters:
instant-epochMillis-- Returns:
-
age
public static TimeDuration age(java.time.ZonedDateTime dt, long epochMillis)
Calculates the age of a datetime against a reference epoch millis. If the datetime is on April 1 and the epochMillis is on April 2, then the age will be a positive 1 day.- Parameters:
dt-epochMillis-- Returns:
-
uptime
public static java.lang.String uptime(java.time.Instant start)
Formats a string of "uptime" relative to NOW such as "6m 1s 467ms"- Parameters:
start-- Returns:
-
uptime
public static java.lang.String uptime(java.lang.Long startEpochMillis)
-
uptime
public static java.lang.String uptime(java.time.ZonedDateTime start)
-
uptime
public static java.lang.String uptime(java.time.ZonedDateTime end, java.time.ZonedDateTime start)
-
uptime
public static java.lang.String uptime(java.time.Instant end, java.time.Instant start)
-
uptime
public static java.lang.String uptime(java.lang.Long endEpochMillis, java.lang.Long startEpochMillis)
-
uptime
public static java.lang.String uptime(java.time.Duration duration)
-
min
public static java.time.Instant min(java.time.Instant a, java.time.Instant b)Returns a if less than or equal to b, otherwise b.- Parameters:
a-b-- Returns:
-
min
public static java.time.Instant min(java.time.Instant... dts)
Returns the minimum of all instants.- Parameters:
dts-- Returns:
-
max
public static java.time.Instant max(java.time.Instant a, java.time.Instant b)Returns a if greater than or equal to b, otherwise b.- Parameters:
a-b-- Returns:
-
max
public static java.time.Instant max(java.time.Instant... dts)
Returns the minimum of all instants.- Parameters:
dts-- Returns:
-
gt
public static boolean gt(java.time.Instant a, java.time.Instant b)Isavalue greater thanb. If a is not null and b is null then this is true.- Parameters:
a-b-- Returns:
-
gte
public static boolean gte(java.time.Instant a, java.time.Instant b)Isavalue greater than or equal tob. Will still be true if both values are null OR if a is non-null and b is null.- Parameters:
a-b-- Returns:
-
lt
public static boolean lt(java.time.Instant a, java.time.Instant b)Isavalue less thanb. If b is not null and a is null then this is true.- Parameters:
a-b-- Returns:
-
lte
public static boolean lte(java.time.Instant a, java.time.Instant b)Isavalue less than or equal tob. Will still be true if both values are null OR if a is non-null and b is null.- Parameters:
a-b-- Returns:
-
within
public static boolean within(java.time.Instant dt, java.time.Instant start, java.time.Instant end)Whether instant is within start and end instants. The min of start and end is detected so the ordering does not matter. By default, this method makes the start date INCLUSIVE and the end date EXCLUSIVE.- Parameters:
dt- The instant to checkstart- The start (or end) of the instant rangeend- The end (or start) of the instant range- Returns:
- True if within range, otherwise false.
-
within
public static boolean within(java.time.Instant dt, java.time.Instant start, java.time.Instant end, boolean endInclusive)Whether instant is within start and end instants.The min of start and end is detected so the ordering does not matter. By default, this method makes the start date INCLUSIVE and the end date EXCLUSIVE/INCLUSIVE depending on what you pass in.- Parameters:
dt- The instant to checkstart- The start (or end) of the instant rangeend- The end (or start) of the instant rangeendInclusive- True if the end date is inclusive, otherwise exclusive.- Returns:
- True if within range, otherwise false.
-
within
public static boolean within(java.time.Instant dt, java.time.Instant start, java.time.Instant end, boolean startInclusive, boolean endInclusive)Whether instant is within start and end instants.The min of start and end is detected so the ordering does not matter.By default, this method makes the start date INCLUSIVE and the end date EXCLUSIVE/INCLUSIVE depending on what you pass in.- Parameters:
dt- The instant to checkstart- The start (or end) of the instant rangeend- The end (or start) of the instant rangestartInclusive- True if the start date is inclusive, otherwise exclusive.endInclusive- True if the end date is inclusive, otherwise exclusive.- Returns:
- True if within range, otherwise false.
-
overlap
public static boolean overlap(java.time.Instant start1, java.time.Instant end1, java.time.Instant start2, java.time.Instant end2)Is there an overlap in range1 (start1toend1) and range2 (start2toend2) null values are treated asInstant.MINfor start andInstant.MAXfor end. By default, this method makes the start date INCLUSIVE and the end date EXCLUSIVE.- Parameters:
start1- - start of range1end1- - end of range1start2- - start of range2end2- - end of range2- Returns:
- boolean
-
overlap
public static boolean overlap(java.time.Instant start1, java.time.Instant end1, java.time.Instant start2, java.time.Instant end2, boolean endInclusive)Is there an overlap in range1 (start1toend1) and range2 (start2toend2) null values are treated asInstant.MINfor start andInstant.MAXfor end. By default, this method makes the start date INCLUSIVE and the end date EXCLUSIVE.- Parameters:
start1- - start of range1end1- - end of range1start2- - start of range2end2- - end of range2endInclusive- True if the end date is inclusive, otherwise exclusive.- Returns:
- boolean
-
overlap
public static boolean overlap(java.time.Instant start1, java.time.Instant end1, java.time.Instant start2, java.time.Instant end2, boolean startInclusive, boolean endInclusive)Is there an overlap in range1 (start1toend1) and range2 (start2toend2) null values are treated asInstant.MINfor start andInstant.MAXfor end. By default, this method makes the start date INCLUSIVE and the end date EXCLUSIVE.- Parameters:
start1- - start of range1end1- - end of range1start2- - start of range2end2- - end of range2startInclusive- True if the start date is inclusive, otherwise exclusive.endInclusive- True if the end date is inclusive, otherwise exclusive.- Returns:
- boolean
-
-