Class TemporalConverter
- java.lang.Object
-
- org.silverpeas.core.date.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 aLocalDate
or anOffsetDateTime
. 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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
TemporalConverter.Conversion<T extends Temporal,R>
A conversion function of a temporal object into a value of type R.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description 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.static Date
asDate(Temporal temporal)
Converts the specified temporal in a Date instance.static Instant
asInstant(Temporal temporal)
Converts the specified temporal in anInstant
instance.static LocalDate
asLocalDate(Temporal temporal)
Converts the specified temporal instance to anLocalDate
instance.static LocalDate
asLocalDate(Temporal temporal, ZoneId zoneId)
Converts the specified temporal instance to anLocalDate
instance after applying the timezone conversion.static OffsetDateTime
asOffsetDateTime(Temporal temporal)
Converts the specified temporal instance to anOffsetDateTime
instance.static ZonedDateTime
asZonedDateTime(Temporal temporal)
Converts the specified temporal instance to aZonedDateTime
instance.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.
-
-
-
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
- aTemporal
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 theTemporal
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 aLocalDate
instance.dateTimeConsumer
- a function that consumers aOffsetDateTime
instance.- Throws:
IllegalArgumentException
- if date parameters are not bothLocalDate
orOffsetDateTime
instances.
-
asOffsetDateTime
public static OffsetDateTime asOffsetDateTime(Temporal temporal)
Converts the specified temporal instance to an
OffsetDateTime
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
OffsetDateTime
instance, then nothing is converted and the temporal is directly returned. If the temporal is aLocalDate
instance then the date is converted into anOffsetDateTime
instance by taking the start of the day in UTC/Greenwich. If the temporal is aLocalDateTime
instance then the time is converted in UTC/Greenwich. If the temporal is anInstant
then it is converted into anOffsetDateTime
instance with the zone offset at UTC/Greenwich.- Parameters:
temporal
- the temporal to convert.- Returns:
- an
OffsetDateTime
instance. - Throws:
IllegalArgumentException
- if the specified temporal is of a type not supported by this converter.
-
asZonedDateTime
public static ZonedDateTime asZonedDateTime(Temporal temporal)
Converts the specified temporal instance to a
ZonedDateTime
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 a
ZonedDateTime
instance, then nothing is converted and the temporal is directly returned. If the temporal is aLocalDate
instance then the date is converted into aZonedDateTime
instance by taking the start of the day in UTC/Greenwich. If the temporal is aLocalDateTime
instance then the time is converted in UTC/Greenwich. If the temporal is anInstant
then it is converted into aZonedDateTime
instance with the zone Id at UTC/Greenwich.- Parameters:
temporal
- the temporal to convert.- Returns:
- a
ZonedDateTime
instance. - Throws:
IllegalArgumentException
- if the specified temporal is of a type not supported by this converter.
-
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 anInstant
then the time part is set at UTC/Greenwich before returning only the date part as aLocalDate
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 anInstant
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. TheDate
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.
-
-