Class FormatCache
- java.lang.Object
-
- de.iip_ecosphere.platform.connectors.formatter.FormatCache
-
public class FormatCache extends java.lang.ObjectCaches formatter instances.- Author:
- Holger Eichelberger, SSE
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classFormatCache.AbstractDateConverter<T>An abstract basic date converter.static interfaceFormatCache.DateConverter<T>Plugin to extend the date time conversion.
-
Field Summary
Fields Modifier and Type Field Description private static java.util.Map<java.lang.Class<?>,FormatCache.DateConverter<?>>CONVERTERSprivate static java.util.Map<java.lang.String,java.text.SimpleDateFormat>DATE_FORMATTERstatic java.lang.StringISO8601_FORMATPre-defined known format for ISO8601.
-
Constructor Summary
Constructors Constructor Description FormatCache()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static <T> java.util.DateconvertToDate(java.lang.Class<T> cls, java.lang.Object data)Uses one of the registered data converters to convertdata.static java.lang.Stringformat(java.util.Date data, java.lang.String format)Formats a date object to a given format.static java.text.SimpleDateFormatgetDateFormatter(java.lang.String format)Returns a (cached) simple date formatter.static java.util.Dateparse(java.lang.Object data, java.lang.String format)Parses a date from the givendatafor the specifiedformat.static voidregisterConverter(FormatCache.DateConverter<?> converter)Registers an additional converter.static voidregisterFormat(java.lang.String name, java.lang.String pattern)Registers a default format forSimpleDateFormat.static java.util.DatetoDate(java.time.LocalDateTime date)Converts aLocalDateTimevalue toDateusing instant and the system default time zone.static java.time.LocalDateTimetoLocalDateTime(java.util.Date date)Converts aDatevalue toLocalDateTimeusing instant and the system default time zone.
-
-
-
Field Detail
-
ISO8601_FORMAT
public static final java.lang.String ISO8601_FORMAT
Pre-defined known format for ISO8601.- See Also:
- Constant Field Values
-
DATE_FORMATTER
private static final java.util.Map<java.lang.String,java.text.SimpleDateFormat> DATE_FORMATTER
-
CONVERTERS
private static final java.util.Map<java.lang.Class<?>,FormatCache.DateConverter<?>> CONVERTERS
-
-
Method Detail
-
registerConverter
public static void registerConverter(FormatCache.DateConverter<?> converter)
Registers an additional converter.- Parameters:
converter- the converter
-
registerFormat
public static void registerFormat(java.lang.String name, java.lang.String pattern)Registers a default format forSimpleDateFormat.- Parameters:
name- the symbolic name of the formatpattern- the pattern format to apply- Throws:
java.lang.IllegalArgumentException- ifpatternis illegal
-
format
public static java.lang.String format(java.util.Date data, java.lang.String format) throws java.io.IOExceptionFormats a date object to a given format.- Parameters:
data- the data/dateformat- the format, may be fromSimpleDateFormatorISO8601_FORMAT- Returns:
- if no format can be constructed
- Throws:
java.io.IOException- if formatting is not possible or the format is unknown
-
convertToDate
private static <T> java.util.Date convertToDate(java.lang.Class<T> cls, java.lang.Object data)Uses one of the registered data converters to convertdata.- Type Parameters:
T- the type ofdata- Parameters:
cls- the type ofdatadata- the data (must not be null)- Returns:
- the converted instance, may be null if there is no conversion
-
parse
public static java.util.Date parse(java.lang.Object data, java.lang.String format) throws java.io.IOExceptionParses a date from the givendatafor the specifiedformat.- Parameters:
data- the data (may be null, the the result will be null)format- the format may be fromSimpleDateFormatorISO8601_FORMAT- Returns:
- the parsed date
- Throws:
java.io.IOException- if parsing is not possible or the format is unknown
-
getDateFormatter
public static java.text.SimpleDateFormat getDateFormatter(java.lang.String format) throws java.io.IOExceptionReturns a (cached) simple date formatter.- Parameters:
format- the format for the requested formatter for a string forSimpleDateFormat- Returns:
- the formatter instance
- Throws:
java.io.IOException- ifformatis not valid
-
toLocalDateTime
public static java.time.LocalDateTime toLocalDateTime(java.util.Date date)
Converts aDatevalue toLocalDateTimeusing instant and the system default time zone.- Parameters:
date- the date to convert (may be null)- Returns:
- the converted date (may be null)
-
toDate
public static java.util.Date toDate(java.time.LocalDateTime date)
Converts aLocalDateTimevalue toDateusing instant and the system default time zone.- Parameters:
date- the date to convert (may be null)- Returns:
- the converted date (may be null)
-
-