Class FormatCache


  • public class FormatCache
    extends java.lang.Object
    Caches formatter instances.
    Author:
    Holger Eichelberger, SSE
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.util.Map<java.lang.Class<?>,​FormatCache.DateConverter<?>> CONVERTERS  
      private static java.util.Map<java.lang.String,​java.text.SimpleDateFormat> DATE_FORMATTER  
      static java.lang.String ISO8601_FORMAT
      Pre-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.Date convertToDate​(java.lang.Class<T> cls, java.lang.Object data)
      Uses one of the registered data converters to convert data.
      static java.lang.String format​(java.util.Date data, java.lang.String format)
      Formats a date object to a given format.
      static java.text.SimpleDateFormat getDateFormatter​(java.lang.String format)
      Returns a (cached) simple date formatter.
      static java.util.Date parse​(java.lang.Object data, java.lang.String format)
      Parses a date from the given data for the specified format.
      static void registerConverter​(FormatCache.DateConverter<?> converter)
      Registers an additional converter.
      static void registerFormat​(java.lang.String name, java.lang.String pattern)
      Registers a default format for SimpleDateFormat.
      static java.util.Date toDate​(java.time.LocalDateTime date)
      Converts a LocalDateTime value to Date using instant and the system default time zone.
      static java.time.LocalDateTime toLocalDateTime​(java.util.Date date)
      Converts a Date value to LocalDateTime using instant and the system default time zone.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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
    • Constructor Detail

      • FormatCache

        public FormatCache()
    • 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 for SimpleDateFormat.
        Parameters:
        name - the symbolic name of the format
        pattern - the pattern format to apply
        Throws:
        java.lang.IllegalArgumentException - if pattern is illegal
      • format

        public static java.lang.String format​(java.util.Date data,
                                              java.lang.String format)
                                       throws java.io.IOException
        Formats a date object to a given format.
        Parameters:
        data - the data/date
        format - the format, may be from SimpleDateFormat or ISO8601_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 convert data.
        Type Parameters:
        T - the type of data
        Parameters:
        cls - the type of data
        data - 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.IOException
        Parses a date from the given data for the specified format.
        Parameters:
        data - the data (may be null, the the result will be null)
        format - the format may be from SimpleDateFormat or ISO8601_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.IOException
        Returns a (cached) simple date formatter.
        Parameters:
        format - the format for the requested formatter for a string for SimpleDateFormat
        Returns:
        the formatter instance
        Throws:
        java.io.IOException - if format is not valid
      • toLocalDateTime

        public static java.time.LocalDateTime toLocalDateTime​(java.util.Date date)
        Converts a Date value to LocalDateTime using 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 a LocalDateTime value to Date using instant and the system default time zone.
        Parameters:
        date - the date to convert (may be null)
        Returns:
        the converted date (may be null)