Package de.xima.fc.utils
Class TemporalUtils
java.lang.Object
de.xima.fc.utils.TemporalUtils
Helper methods for classes in the
java.time
package.- Author:
- XIMA MEDIA GmbH
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
Checks whether two durations are equal to within the given threshold.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
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 Details
-
ONE_SECOND
A duration of one second.
-
-
Method Details
-
equals
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.
-
plusNoWrapAround
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
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
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
- 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
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
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
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.
-
toZonedDateTime
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.
-
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.
-
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
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.
-
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.
-
max
- 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.
-