Package com.cedarsoftware.util
Class SafeSimpleDateFormat
java.lang.Object
java.text.Format
java.text.DateFormat
com.cedarsoftware.util.SafeSimpleDateFormat
- All Implemented Interfaces:
Serializable
,Cloneable
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:
-
Nested Class Summary
Nested classes/interfaces inherited from class java.text.DateFormat
DateFormat.Field
-
Field Summary
Fields inherited from class java.text.DateFormat
AM_PM_FIELD, calendar, DATE_FIELD, DAY_OF_WEEK_FIELD, DAY_OF_WEEK_IN_MONTH_FIELD, DAY_OF_YEAR_FIELD, DEFAULT, ERA_FIELD, FULL, HOUR_OF_DAY0_FIELD, HOUR_OF_DAY1_FIELD, HOUR0_FIELD, HOUR1_FIELD, LONG, MEDIUM, MILLISECOND_FIELD, MINUTE_FIELD, MONTH_FIELD, numberFormat, SECOND_FIELD, SHORT, TIMEZONE_FIELD, WEEK_OF_MONTH_FIELD, WEEK_OF_YEAR_FIELD, YEAR_FIELD
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
Clears the static accessor's per-thread cache.void
Clears this instance’s cached formatter for the current thread.static void
Clears all cached formatters for the current thread (instance-based cache).boolean
format
(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) static SimpleDateFormat
getDateFormat
(String pattern) Legacy static accessor preserved for compatibility.int
hashCode()
parse
(String source, ParsePosition pos) void
set2DigitYearStart
(Date date) void
setCalendar
(Calendar cal) void
setDateFormatSymbols
(DateFormatSymbols symbols) void
setLenient
(boolean lenient) void
setNumberFormat
(NumberFormat format) void
setTimeZone
(TimeZone tz) toString()
Methods inherited from class java.text.DateFormat
clone, format, format, getAvailableLocales, getCalendar, getDateInstance, getDateInstance, getDateInstance, getDateTimeInstance, getDateTimeInstance, getDateTimeInstance, getInstance, getNumberFormat, getTimeInstance, getTimeInstance, getTimeInstance, getTimeZone, isLenient, parse, parseObject
Methods inherited from class java.text.Format
format, formatToCharacterIterator, parseObject
-
Constructor Details
-
SafeSimpleDateFormat
-
-
Method Details
-
getDateFormat
Legacy static accessor preserved for compatibility. Returns a per-thread cachedSimpleDateFormat
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
- Specified by:
format
in classDateFormat
-
parse
- Specified by:
parse
in classDateFormat
-
setTimeZone
- Overrides:
setTimeZone
in classDateFormat
-
setLenient
public void setLenient(boolean lenient) - Overrides:
setLenient
in classDateFormat
-
setCalendar
- Overrides:
setCalendar
in classDateFormat
-
setNumberFormat
- Overrides:
setNumberFormat
in classDateFormat
-
setDateFormatSymbols
-
set2DigitYearStart
-
toString
-
equals
- Overrides:
equals
in classDateFormat
-
hashCode
public int hashCode()- Overrides:
hashCode
in classDateFormat
-
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.
-