Class SafeSimpleDateFormat

java.lang.Object
java.text.Format
java.text.DateFormat
com.cedarsoftware.util.SafeSimpleDateFormat
All Implemented Interfaces:
Serializable, Cloneable

public class SafeSimpleDateFormat extends DateFormat
Thread-safe wrapper for SimpleDateFormat with copy-on-write semantics.

Design goals:

  • Source/binary compatible surface for existing users.
  • Re-entrant and safe across threads (no shared mutable SDF instances).
  • Performance: at most one SimpleDateFormat per thread per configuration.
  • Mutators create a new immutable State; threads lazily rebuild their SDF (copy-on-write).

Hot path: no locks. Lookups happen in a per-thread LRU.

For legacy code that used SafeSimpleDateFormat.getDateFormat(pattern), this class still provides the static accessor, returning a thread-local SimpleDateFormat keyed only by pattern (same semantics as before).

Author:
John DeRegnaucourt ([email protected])
Copyright (c) Cedar Software LLC

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

License

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
See Also:
  • Constructor Details

    • SafeSimpleDateFormat

      public SafeSimpleDateFormat(String format)
  • Method Details

    • getDateFormat

      public static SimpleDateFormat getDateFormat(String pattern)
      Legacy static accessor preserved for compatibility. Returns a per-thread cached SimpleDateFormat for the given pattern.

      Note: Mutating the returned SimpleDateFormat (e.g., setTimeZone) will affect subsequent uses of this pattern in the same thread, just like the original implementation.

    • clearStaticThreadLocalCache

      public static void clearStaticThreadLocalCache()
      Clears the static accessor's per-thread cache.
    • format

      public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition)
      Specified by:
      format in class DateFormat
    • parse

      public Date parse(String source, ParsePosition pos)
      Specified by:
      parse in class DateFormat
    • setTimeZone

      public void setTimeZone(TimeZone tz)
      Overrides:
      setTimeZone in class DateFormat
    • setLenient

      public void setLenient(boolean lenient)
      Overrides:
      setLenient in class DateFormat
    • setCalendar

      public void setCalendar(Calendar cal)
      Overrides:
      setCalendar in class DateFormat
    • setNumberFormat

      public void setNumberFormat(NumberFormat format)
      Overrides:
      setNumberFormat in class DateFormat
    • setDateFormatSymbols

      public void setDateFormatSymbols(DateFormatSymbols symbols)
    • set2DigitYearStart

      public void set2DigitYearStart(Date date)
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object other)
      Overrides:
      equals in class DateFormat
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class DateFormat
    • clearThreadLocalCache

      public static void clearThreadLocalCache()
      Clears all cached formatters for the current thread (instance-based cache).
    • clearThreadLocal

      public void clearThreadLocal()
      Clears this instance’s cached formatter for the current thread.