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
SimpleDateFormatper 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 voidClears the static accessor's per-thread cache.voidClears this instance’s cached formatter for the current thread.static voidClears all cached formatters for the current thread (instance-based cache).booleanformat(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) static SimpleDateFormatgetDateFormat(String pattern) Legacy static accessor preserved for compatibility.inthashCode()parse(String source, ParsePosition pos) voidset2DigitYearStart(Date date) voidsetCalendar(Calendar cal) voidsetDateFormatSymbols(DateFormatSymbols symbols) voidsetLenient(boolean lenient) voidsetNumberFormat(NumberFormat format) voidsetTimeZone(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, parseObjectMethods 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 cachedSimpleDateFormatfor 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:
formatin classDateFormat
-
parse
- Specified by:
parsein classDateFormat
-
setTimeZone
- Overrides:
setTimeZonein classDateFormat
-
setLenient
public void setLenient(boolean lenient) - Overrides:
setLenientin classDateFormat
-
setCalendar
- Overrides:
setCalendarin classDateFormat
-
setNumberFormat
- Overrides:
setNumberFormatin classDateFormat
-
setDateFormatSymbols
-
set2DigitYearStart
-
toString
-
equals
- Overrides:
equalsin classDateFormat
-
hashCode
public int hashCode()- Overrides:
hashCodein 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.
-