Class TemporalConverter


  • public class TemporalConverter
    extends Object
    A converter of date and datetime into different temporal types used in Silverpeas. It provides methods to perform operations against temporal objects according their concrete type; the temporal objects are then converted before passing them to the matching function. It provides also convenient methods to convert a temporal, whatever its type, to another type like a LocalDate or an OffsetDateTime. It provides also methods dedicated to convert a string representation of a date or a datetime to its corresponding temporal instance and vice-versa.
    Author:
    mmoquillon
    • Method Detail

      • applyByType

        @SafeVarargs
        public static <T> T applyByType​(Temporal temporal,
                                        TemporalConverter.Conversion<? extends Temporal,​T>... conversions)
        Performs one of the specified conversion function against the given temporal object according to its concrete type. The result of the conversion is then returned.
        Type Parameters:
        T - the concrete type of the converted value.
        Parameters:
        temporal - a Temporal object to convert.
        conversions - one or more conversion functions.
        Returns:
        the value obtained by the application of the correct conversion function to the given Temporal object.
        Throws:
        IllegalArgumentException - exception if no conversion functions are passed or the concrete type of the Temporal object isn't accepted by any of the conversion functions.
      • consumeByType

        public static void consumeByType​(Temporal temporal,
                                         Consumer<LocalDate> dateConsumer,
                                         Consumer<OffsetDateTime> dateTimeConsumer)
        Performs one of the specified consumer according to the type of the specified temporal object. The given temporal object is converted before passing it to one of the specified consumers.
        Parameters:
        temporal - a temporal object to consume.
        dateConsumer - a function that consumes a LocalDate instance.
        dateTimeConsumer - a function that consumers a OffsetDateTime instance.
        Throws:
        IllegalArgumentException - if date parameters are not both LocalDate or OffsetDateTime instances.
      • asLocalDate

        public static LocalDate asLocalDate​(Temporal temporal)

        Converts the specified temporal instance to an LocalDate instance. The temporal instance must be of one of the following type:

        Any other types aren't supported and as such an IllegalArgumentException is thrown.

        If the temporal is already an LocalDate instance, then nothing is converted and the temporal is directly returned. If the temporal is a datetime, then only the date is returned. If the temporal is an Instant then the time part is set at UTC/Greenwich before returning only the date part as a LocalDate instance.

        Parameters:
        temporal - the temporal to convert.
        Returns:
        an LocalDate instance.
        Throws:
        IllegalArgumentException - if the specified temporal is of a type not supported by this converter.
      • asLocalDate

        public static LocalDate asLocalDate​(Temporal temporal,
                                            ZoneId zoneId)

        Converts the specified temporal instance to an LocalDate instance after applying the timezone conversion. The temporal instance must be of one of the following type:

        Any other types aren't supported and as such an IllegalArgumentException is thrown.

        If the temporal is already an LocalDate instance, then nothing is converted and the temporal is directly returned. If the temporal is a datetime, then it is converted to the specified timezone for a same instant before returning only the date (the day can change once the timezone applied onto the time).

        Parameters:
        temporal - the temporal to convert.
        Returns:
        an LocalDate instance.
        Throws:
        IllegalArgumentException - if the specified temporal is of a type not supported by this converter.
      • asInstant

        public static Instant asInstant​(Temporal temporal)
        Converts the specified temporal in an Instant instance. If the temporal is a date then it is expressed as a datetime in UTC/Greenwich. If the temporal is a local datetime, then the time is expressed in UTC/Greenwich.
        Parameters:
        temporal - the temporal to convert.
        Returns:
        an Instant instance.
      • asDate

        public static Date asDate​(Temporal temporal)
        Converts the specified temporal in a Date instance. The Date instance is created from the number of milliseconds of the temporal from the epoch of 1970-01-01T00:00:00Z.

        Before getting the number of milliseconds from the epoch of 1970-01-01T00:00:00Z, some timezone conversions can be required. If the temporal is a date then it is converted into a datetime in UTC/Greenwich. If the temporal is a local datetime, therefore the time is considered expressed in the default timezone and hence the time is converted in UTC/Greenwich. For any others temporal value, their timezone is taken into account to convert the time in UTC/Greenwich.

        Parameters:
        temporal - a date or a datetime.
        Returns:
        a Date instance.