Class FixedDateFormat
- java.lang.Object
-
- org.apache.logging.log4j.core.util.datetime.FixedDateFormat
-
public class FixedDateFormat extends java.lang.Object
Custom time formatter that trades flexibility for performance. This formatter only supports the date patterns defined inFixedDateFormat.FixedFormat
. For any other date patterns useFastDateFormat
.Related benchmarks: /log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/TimeFormatBenchmark.java and /log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/ThreadsafeDateFormatBenchmark.java
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
FixedDateFormat.FixedFormat
Enumeration over the supported date/time format patterns.static class
FixedDateFormat.FixedTimeZoneFormat
Fixed time zone formats.
-
Field Summary
Fields Modifier and Type Field Description private char[]
cachedDate
private int
dateLength
private int[]
dstOffsets
private FastDateFormat
fastDateFormat
private FixedDateFormat.FixedFormat
fixedFormat
private FixedDateFormat.FixedTimeZoneFormat
fixedTimeZoneFormat
private int
length
private long
midnightToday
private long
midnightTomorrow
private char
millisSeparatorChar
private int
millisSeparatorLength
private static char
NONE
private int
secondFractionDigits
(package private) static int[]
TABLE
private char
timeSeparatorChar
private int
timeSeparatorLength
private java.util.TimeZone
timeZone
-
Constructor Summary
Constructors Constructor Description FixedDateFormat(FixedDateFormat.FixedFormat fixedFormat, java.util.TimeZone tz)
Constructs a FixedDateFormat for the specified fixed format.FixedDateFormat(FixedDateFormat.FixedFormat fixedFormat, java.util.TimeZone tz, int secondFractionDigits)
Constructs a FixedDateFormat for the specified fixed format.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private long
calcMidnightMillis(long time, int addDays)
static FixedDateFormat
create(FixedDateFormat.FixedFormat format)
Returns a newFixedDateFormat
object for the specifiedFixedFormat
and aTimeZone.getDefault()
TimeZone.static FixedDateFormat
create(FixedDateFormat.FixedFormat format, java.util.TimeZone tz)
Returns a newFixedDateFormat
object for the specifiedFixedFormat
and TimeZone.static FixedDateFormat
createIfSupported(java.lang.String... options)
private int
daylightSavingTime(int hourOfDay)
private int
digitsLessThanThree()
java.lang.String
format(long epochMillis)
int
format(long epochMillis, char[] buffer, int startPos)
java.lang.String
formatInstant(Instant instant)
int
formatInstant(Instant instant, char[] buffer, int startPos)
private int
formatNanoOfMillisecond(int nanoOfMillisecond, char[] buffer, int pos)
java.lang.String
getFormat()
Returns the full pattern of the selected fixed format.java.util.TimeZone
getTimeZone()
Returns the time zone.boolean
isEquivalent(long oldEpochSecond, int oldNanoOfSecond, long epochSecond, int nanoOfSecond)
Returnstrue
if the old and new date values will result in the same formatted output,false
if results may differ.long
millisSinceMidnight(long currentTime)
Returns the number of milliseconds since midnight in the time zone that thisFixedDateFormat
was constructed with for the specified currentTime.private void
updateCachedDate(long now)
private void
updateDaylightSavingTime()
private void
updateMidnightMillis(long now)
private void
writeDate(char[] buffer, int startPos)
private int
writeTime(int ms, char[] buffer, int pos)
private int
writeTimeZone(long epochMillis, char[] buffer, int pos)
-
-
-
Field Detail
-
NONE
private static final char NONE
- See Also:
- Constant Field Values
-
fixedFormat
private final FixedDateFormat.FixedFormat fixedFormat
-
timeZone
private final java.util.TimeZone timeZone
-
length
private final int length
-
secondFractionDigits
private final int secondFractionDigits
-
fastDateFormat
private final FastDateFormat fastDateFormat
-
timeSeparatorChar
private final char timeSeparatorChar
-
millisSeparatorChar
private final char millisSeparatorChar
-
timeSeparatorLength
private final int timeSeparatorLength
-
millisSeparatorLength
private final int millisSeparatorLength
-
fixedTimeZoneFormat
private final FixedDateFormat.FixedTimeZoneFormat fixedTimeZoneFormat
-
midnightToday
private volatile long midnightToday
-
midnightTomorrow
private volatile long midnightTomorrow
-
dstOffsets
private final int[] dstOffsets
-
cachedDate
private char[] cachedDate
-
dateLength
private int dateLength
-
TABLE
static int[] TABLE
-
-
Constructor Detail
-
FixedDateFormat
FixedDateFormat(FixedDateFormat.FixedFormat fixedFormat, java.util.TimeZone tz)
Constructs a FixedDateFormat for the specified fixed format.Package protected for unit tests.
- Parameters:
fixedFormat
- the fixed formattz
- time zone
-
FixedDateFormat
FixedDateFormat(FixedDateFormat.FixedFormat fixedFormat, java.util.TimeZone tz, int secondFractionDigits)
Constructs a FixedDateFormat for the specified fixed format.Package protected for unit tests.
- Parameters:
fixedFormat
- the fixed formattz
- time zonesecondFractionDigits
- the number of digits specifying the fraction of the second to show
-
-
Method Detail
-
createIfSupported
public static FixedDateFormat createIfSupported(java.lang.String... options)
-
create
public static FixedDateFormat create(FixedDateFormat.FixedFormat format)
Returns a newFixedDateFormat
object for the specifiedFixedFormat
and aTimeZone.getDefault()
TimeZone.- Parameters:
format
- the format to use- Returns:
- a new
FixedDateFormat
object
-
create
public static FixedDateFormat create(FixedDateFormat.FixedFormat format, java.util.TimeZone tz)
Returns a newFixedDateFormat
object for the specifiedFixedFormat
and TimeZone.- Parameters:
format
- the format to usetz
- the time zone to use- Returns:
- a new
FixedDateFormat
object
-
getFormat
public java.lang.String getFormat()
Returns the full pattern of the selected fixed format.- Returns:
- the full date-time pattern
-
getTimeZone
public java.util.TimeZone getTimeZone()
Returns the time zone.- Returns:
- the time zone
-
millisSinceMidnight
public long millisSinceMidnight(long currentTime)
Returns the number of milliseconds since midnight in the time zone that this
FixedDateFormat
was constructed with for the specified currentTime.As a side effect, this method updates the cached formatted date and the cached date demarcation timestamps when the specified current time is outside the previously set demarcation timestamps for the start or end of the current day.
- Parameters:
currentTime
- the current time in millis since the epoch- Returns:
- the number of milliseconds since midnight for the specified time
-
updateMidnightMillis
private void updateMidnightMillis(long now)
-
calcMidnightMillis
private long calcMidnightMillis(long time, int addDays)
-
updateDaylightSavingTime
private void updateDaylightSavingTime()
-
updateCachedDate
private void updateCachedDate(long now)
-
formatInstant
public java.lang.String formatInstant(Instant instant)
-
formatInstant
public int formatInstant(Instant instant, char[] buffer, int startPos)
-
digitsLessThanThree
private int digitsLessThanThree()
-
format
public java.lang.String format(long epochMillis)
-
format
public int format(long epochMillis, char[] buffer, int startPos)
-
writeDate
private void writeDate(char[] buffer, int startPos)
-
writeTime
private int writeTime(int ms, char[] buffer, int pos)
-
writeTimeZone
private int writeTimeZone(long epochMillis, char[] buffer, int pos)
-
formatNanoOfMillisecond
private int formatNanoOfMillisecond(int nanoOfMillisecond, char[] buffer, int pos)
-
daylightSavingTime
private int daylightSavingTime(int hourOfDay)
-
isEquivalent
public boolean isEquivalent(long oldEpochSecond, int oldNanoOfSecond, long epochSecond, int nanoOfSecond)
Returnstrue
if the old and new date values will result in the same formatted output,false
if results may differ.
-
-