Class ThreadSafeSimpleDateFormat


  • public class ThreadSafeSimpleDateFormat
    extends java.lang.Object
    Wrapper around java.text.SimpleDateFormat that can be called by multiple threads concurrently.

    SimpleDateFormat has a high overhead in creating and is not thread safe. To make best use of resources, the ThreadSafeSimpleDateFormat provides a dynamically sizing pool of instances, each of which will only be called by a single thread at a time.

    The pool has a maximum capacity, to limit overhead. If all instances in the pool are in use and another is required, it shall block until one becomes available.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.String formatString  
      private Pool pool  
      private java.util.TimeZone timeZone  
    • Constructor Summary

      Constructors 
      Constructor Description
      ThreadSafeSimpleDateFormat​(java.lang.String format, java.util.TimeZone timeZone, int initialPoolSize, int maxPoolSize, boolean lenient)  
      ThreadSafeSimpleDateFormat​(java.lang.String format, java.util.TimeZone timeZone, java.util.Locale locale, int initialPoolSize, int maxPoolSize, boolean lenient)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private java.text.DateFormat fetchFromPool()  
      java.lang.String format​(java.util.Date date)  
      java.util.Date parse​(java.lang.String date)  
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • formatString

        private final java.lang.String formatString
      • pool

        private final Pool pool
      • timeZone

        private final java.util.TimeZone timeZone
    • Constructor Detail

      • ThreadSafeSimpleDateFormat

        public ThreadSafeSimpleDateFormat​(java.lang.String format,
                                          java.util.TimeZone timeZone,
                                          int initialPoolSize,
                                          int maxPoolSize,
                                          boolean lenient)
      • ThreadSafeSimpleDateFormat

        public ThreadSafeSimpleDateFormat​(java.lang.String format,
                                          java.util.TimeZone timeZone,
                                          java.util.Locale locale,
                                          int initialPoolSize,
                                          int maxPoolSize,
                                          boolean lenient)
    • Method Detail

      • format

        public java.lang.String format​(java.util.Date date)
      • parse

        public java.util.Date parse​(java.lang.String date)
                             throws java.text.ParseException
        Throws:
        java.text.ParseException
      • fetchFromPool

        private java.text.DateFormat fetchFromPool()
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object