Class InstantPatternDynamicFormatter
- All Implemented Interfaces:
InstantFormatter, InstantPatternFormatter
InstantPatternFormatter that uses DateTimeFormatter under the hood.
The pattern is analyzed and parts that require a precision lower than or equal to are precomputed, cached, and updated once every .
The rest is computed dynamically.
For instance, given the pattern yyyy-MM-dd'T'HH:mm:ss.SSSX, the generated formatter will
- Sequence the pattern and assign a time precision to each part (e.g.,
MMis of month precision) - Precompute and cache the output for parts that are of precision lower than or equal to (i.e.,
yyyy-MM-dd'T'HH:mm:andX) and cache it - Upon a formatting request, combine the cached outputs with the dynamic parts (i.e.,
ss.SSS)
- Since:
- 2.25.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static class(package private) static final classCreates formatters that useDateTimeFormatter.(package private) static class(package private) static class(package private) static final classprivate static final classNested classes/interfaces inherited from interface InstantPatternFormatter
InstantPatternFormatter.Builder -
Field Summary
FieldsFields inherited from interface InstantPatternFormatter
LEGACY_FORMATTERS_ENABLED -
Constructor Summary
ConstructorsConstructorDescriptionInstantPatternDynamicFormatter(String pattern, Locale locale, TimeZone timeZone) -
Method Summary
Modifier and TypeMethodDescriptionprivate static InstantPatternFormattercreateFormatter(String pattern, Locale locale, TimeZone timeZone, ChronoUnit precisionThreshold, Instant creationInstant) private static InstantPatternDynamicFormatter.TimestampedFormattercreateTimestampedFormatter(String pattern, Locale locale, TimeZone timeZone, @Nullable Instant creationInstant) voidformatTo(StringBuilder buffer, Instant instant) private InstantPatternFormattergetEffectiveFormatter(Instant instant) private static InstantPatternDynamicFormatter.PatternSequencegetStaticPatternSequence(String pattern, int startIndex, int endIndex) private static booleanisDynamicPatternLetter(char c) private static List<InstantPatternDynamicFormatter.PatternSequence> mergeFactories(List<InstantPatternDynamicFormatter.PatternSequence> sequences, ChronoUnit precisionThreshold) Merges pattern sequences usingInstantPatternDynamicFormatter.PatternSequence.tryMerge(InstantPatternDynamicFormatter.PatternSequence, ChronoUnit).private static ChronoUnitmin(ChronoUnit left, ChronoUnit right) private static List<InstantPatternDynamicFormatter.PatternSequence> sequencePattern(String pattern) (package private) static List<InstantPatternDynamicFormatter.PatternSequence> sequencePattern(String pattern, ChronoUnit precisionThreshold) private static longtoEpochMinutes(Instant instant) private static TemporalAccessortoTemporalAccessor(Instant instant) Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface InstantFormatter
format
-
Field Details
-
PRECISION_THRESHOLD
-
timestampedFormatterRef
private final AtomicReference<InstantPatternDynamicFormatter.TimestampedFormatter> timestampedFormatterRef
-
-
Constructor Details
-
InstantPatternDynamicFormatter
-
-
Method Details
-
getPattern
- Specified by:
getPatternin interfaceInstantPatternFormatter
-
getLocale
- Specified by:
getLocalein interfaceInstantPatternFormatter
-
getTimeZone
- Specified by:
getTimeZonein interfaceInstantPatternFormatter
-
getPrecision
- Specified by:
getPrecisionin interfaceInstantFormatter- Returns:
- the time precision of the formatted output
-
formatTo
- Specified by:
formatToin interfaceInstantFormatter
-
getEffectiveFormatter
-
createTimestampedFormatter
private static InstantPatternDynamicFormatter.TimestampedFormatter createTimestampedFormatter(String pattern, Locale locale, TimeZone timeZone, @Nullable Instant creationInstant) -
createFormatter
private static InstantPatternFormatter createFormatter(String pattern, Locale locale, TimeZone timeZone, ChronoUnit precisionThreshold, Instant creationInstant) -
min
-
sequencePattern
static List<InstantPatternDynamicFormatter.PatternSequence> sequencePattern(String pattern, ChronoUnit precisionThreshold) -
sequencePattern
-
getStaticPatternSequence
private static InstantPatternDynamicFormatter.PatternSequence getStaticPatternSequence(String pattern, int startIndex, int endIndex) -
isDynamicPatternLetter
private static boolean isDynamicPatternLetter(char c) -
mergeFactories
private static List<InstantPatternDynamicFormatter.PatternSequence> mergeFactories(List<InstantPatternDynamicFormatter.PatternSequence> sequences, ChronoUnit precisionThreshold) Merges pattern sequences usingInstantPatternDynamicFormatter.PatternSequence.tryMerge(InstantPatternDynamicFormatter.PatternSequence, ChronoUnit).Example
For example, given the
yyyy-MM-dd'T'HH:mm:ss.SSSpattern, a precision threshold ofChronoUnit.MINUTESand the three implementations (InstantPatternDynamicFormatter.DynamicPatternSequence,InstantPatternDynamicFormatter.StaticPatternSequenceandInstantPatternDynamicFormatter.SecondPatternSequence) from this class, this method will combine pattern sequences associated withyyyy-MM-dd'T'HH:mm:into a single sequence, since these are consecutive and effectively constant sequences.[ dateTimeFormatter(pattern="yyyy", precision=YEARS), static(literal="-"), dateTimeFormatter(pattern="MM", precision=MONTHS), static(literal="-"), dateTimeFormatter(pattern="dd", precision=DAYS), static(literal="T"), dateTimeFormatter(pattern="HH", precision=HOURS), static(literal=":"), dateTimeFormatter(pattern="mm", precision=MINUTES), static(literal=":"), second(pattern="ss", precision=SECONDS), static(literal="."), second(pattern="SSS", precision=MILLISECONDS) dateTimeFormatter(pattern="X", precision=HOURS), ]The above sequencing implies creation of 12
InstantPatternDynamicFormatter.AbstractFormatters. This method transforms it to the following:[ dateTimeFormatter(pattern="yyyy-MM-dd'T'HH:mm", precision=MINUTES), second(pattern="ss.SSS", precision=MILLISECONDS), dateTimeFormatter(pattern="X", precision=MINUTES) ]The resultant sequencing effectively translates to 3
InstantPatternDynamicFormatter.AbstractFormatters.- Parameters:
sequences- a list of pattern formatter factoriesprecisionThreshold- a precision threshold to determine effectively constant sequences- Returns:
- transformed sequencing, where sequences that are effectively constant or effectively dynamic are merged.
-
toEpochMinutes
-
toTemporalAccessor
-