Package de.xima.fc.utils
Class TemporalUtils
- java.lang.Object
-
- de.xima.fc.utils.TemporalUtils
-
-
Field Summary
Fields Modifier and Type Field Description static Duration
ONE_SECOND
A duration of one second.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static boolean
equals(Duration lhs, Duration rhs, Duration threshold)
Checks whether two durations are equal to within the given threshold.static Instant
instantToMicrosecondPrecision(Instant instant)
Converts the given instant to millisecond precision, i.e.static <T extends TemporalAccessor & Comparable<T>>
Tmax(T lhs, T rhs)
static LocalTime
plusNoWrapAround(LocalTime time, Duration amountToAdd)
Adds the given duration to the given local time, without wrap around.static LocalTime
plusNullOnWrapAround(LocalTime time, Duration amountToAdd)
Adds the given duration to the given local time, without wrap around.static ZonedDateTime
plusNullOnWrapAround(ZonedDateTime dateTime, Duration amountToAdd)
Same specification asplusNoWrapAround(LocalTime, Duration)
, but with zoned date times instead.static DayOfWeek
toDayOfWeek(Instant instant, ZoneId targetZone)
static Instant
toInstant(LocalDate sourceDate, LocalTime sourceTime, ZoneId sourceZone)
Converts the date time from the source time zone to an instant.static Instant
toInstant(LocalDateTime sourceDateTime, ZoneId sourceZone)
Converts the date time from the source time zone to an instant.static LocalDateTime
toLocalDateTime(Instant instant, ZoneId targetZone)
Converts an instant to a local date time in a given time zone.static LocalDateTime
toLocalDateTime(LocalDate sourceDate, LocalTime sourceTime, ZoneId sourceZone, ZoneId targetZone)
Converts the date time from the source time zone to the target time zone.static LocalDateTime
toLocalDateTime(LocalDateTime sourceDateTime, ZoneId sourceZone, ZoneId targetZone)
Converts the date time from the source time zone to the target time zone.static ZonedDateTime
toZonedDateTime(Instant instant, ZoneId targetZone)
Converts an instant to a zoned date time in a given time zone.static ZonedDateTime
toZonedDateTime(LocalDate sourceDate, LocalTime sourceTime, ZoneId sourceZone, ZoneId targetZone)
Converts the date time from the source time zone to the target time zone.static ZonedDateTime
toZonedDateTime(ZonedDateTime sourceDateTime, ZoneId targetZone)
Converts the date time from the source time zone to the target time zone.
-
-
-
Field Detail
-
ONE_SECOND
public static final Duration ONE_SECOND
A duration of one second.
-
-
Method Detail
-
equals
public static boolean equals(Duration lhs, Duration rhs, Duration threshold)
Checks whether two durations are equal to within the given threshold.- Parameters:
lhs
- First duration to compare.null
is consideredDuration.ZERO
.rhs
- Second duration to compare.null
is consideredDuration.ZERO
.threshold
- Threshold for the comparison.- Returns:
true
if|rhs-lhs| <= threshold
, orfalse
otherwise.
-
instantToMicrosecondPrecision
public static Instant instantToMicrosecondPrecision(Instant instant)
Converts the given instant to millisecond precision, i.e. rounds down to the nearest microsecond.- Parameters:
instant
- Instant to convert.- Returns:
- The given instant, rounded down to the nearest microsecond.
-
max
public static <T extends TemporalAccessor & Comparable<T>> T max(T lhs, T rhs)
- Type Parameters:
T
- Type of the temporal.- Parameters:
lhs
- First temporal to compare.rhs
- Second temporal to compare.- Returns:
- The temporal that represents a point in time further in the future.
-
plusNoWrapAround
public static LocalTime plusNoWrapAround(LocalTime time, Duration amountToAdd)
Adds the given duration to the given local time, without wrap around.- 01:00 + 30min = 01:30
- 23:30 + 60min = 23:59 (
LocalTime.MAX
) - 00:30 - 60min = 00:00
- 04:00 + 40h = 23:59 (
LocalTime.MAX
)
- Parameters:
time
- Time to which to add an amount.amountToAdd
- Amount to add.- Returns:
- The result of adding the given amount to the given time, capped at
00:00
and23:59
.
-
plusNullOnWrapAround
public static LocalTime plusNullOnWrapAround(LocalTime time, Duration amountToAdd)
Adds the given duration to the given local time, without wrap around.- 01:00 + 30min = 01:30
- 01:00 - 60min = 00:00
- 23:30 + 30min =
LocalTime.MAX
- 23:30 + 60min = null
- 00:30 - 60min = null
- 04:00 + 40hours = null
- Parameters:
time
- Time to which to add an amount.amountToAdd
- Amount to add.- Returns:
- The result of adding the given amount to the given time, capped at
00:00
and23:59
.
-
plusNullOnWrapAround
public static ZonedDateTime plusNullOnWrapAround(ZonedDateTime dateTime, Duration amountToAdd)
Same specification asplusNoWrapAround(LocalTime, Duration)
, but with zoned date times instead.- Parameters:
dateTime
- Time to which to add an amount.amountToAdd
- Amount to add.- Returns:
- The result of adding the given amount to the given time, capped at
00:00
and23:59
.
-
toDayOfWeek
public static DayOfWeek toDayOfWeek(Instant instant, ZoneId targetZone)
- Parameters:
instant
- An instant for which to get the day of week.targetZone
- Target time zone in which to get the day of week.- Returns:
- The day of week of the given instant, as measured in the given target time zone.
-
toInstant
public static Instant toInstant(LocalDate sourceDate, LocalTime sourceTime, ZoneId sourceZone)
Converts the date time from the source time zone to an instant.- Parameters:
sourceDate
- Date to convert.sourceTime
- Time to convert.sourceZone
- Source time zone.- Returns:
- The given source date time, converted form the source time zone to an instant.
-
toInstant
public static Instant toInstant(LocalDateTime sourceDateTime, ZoneId sourceZone)
Converts the date time from the source time zone to an instant.- Parameters:
sourceDateTime
- Date time to convert.sourceZone
- Source time zone.- Returns:
- The given source date time, converted form the source time zone to an instant.
-
toLocalDateTime
public static LocalDateTime toLocalDateTime(Instant instant, ZoneId targetZone)
Converts an instant to a local date time in a given time zone.- Parameters:
instant
- An instant to convert.targetZone
- Target time zone.- Returns:
- The date and time of the instant in the target time zone.
-
toLocalDateTime
public static LocalDateTime toLocalDateTime(LocalDate sourceDate, LocalTime sourceTime, ZoneId sourceZone, ZoneId targetZone)
Converts the date time from the source time zone to the target time zone.- Parameters:
sourceDate
- Date to convert.sourceTime
- Time to convert.sourceZone
- Source time zone.targetZone
- Target time zone.- Returns:
- The given source date time, converted form the source time zone to the target time zone.
-
toLocalDateTime
public static LocalDateTime toLocalDateTime(LocalDateTime sourceDateTime, ZoneId sourceZone, ZoneId targetZone)
Converts the date time from the source time zone to the target time zone.- Parameters:
sourceDateTime
- Date time to convert.sourceZone
- Source time zone.targetZone
- Target time zone.- Returns:
- The given source date time, converted form the source time zone to the target time zone.
-
toZonedDateTime
public static ZonedDateTime toZonedDateTime(Instant instant, ZoneId targetZone)
Converts an instant to a zoned date time in a given time zone.- Parameters:
instant
- An instant to convert.targetZone
- Target time zone.- Returns:
- The date and time of the instant in the target time zone.
-
toZonedDateTime
public static ZonedDateTime toZonedDateTime(LocalDate sourceDate, LocalTime sourceTime, ZoneId sourceZone, ZoneId targetZone)
Converts the date time from the source time zone to the target time zone.- Parameters:
sourceDate
- Date to convert.sourceTime
- Time to convert.sourceZone
- Source time zone.targetZone
- Target time zone.- Returns:
- The given source date time, converted form the source time zone to the target time zone.
-
toZonedDateTime
public static ZonedDateTime toZonedDateTime(ZonedDateTime sourceDateTime, ZoneId targetZone)
Converts the date time from the source time zone to the target time zone.- Parameters:
sourceDateTime
- Source date time to convert.targetZone
- Target time zone.- Returns:
- The given source date time, converted form the source time zone to the target time zone.
-
-