Class TemporalUtils


  • public class TemporalUtils
    extends Object
    Helper methods for classes in the java.time package.
    Author:
    XIMA MEDIA GmbH
    • 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 considered Duration.ZERO.
        rhs - Second duration to compare. null is considered Duration.ZERO.
        threshold - Threshold for the comparison.
        Returns:
        true if |rhs-lhs| <= threshold, or false otherwise.
      • plusNoWrapAround

        public static LocalTime plusNoWrapAround​(LocalTime time,
                                                 Duration amountToAdd)
        Adds the given duration to the given local time, without wrap around.
        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 and 23: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 and 23:59.
      • plusNullOnWrapAround

        public static ZonedDateTime plusNullOnWrapAround​(ZonedDateTime dateTime,
                                                         Duration amountToAdd)
        Same specification as plusNoWrapAround(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 and 23: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.
      • 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.
      • 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

        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.
      • 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

        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.