Top Description Fields Constructors Methods
com.fasterxml.jackson.databind.util

public Class StdDateFormat

extends DateFormat
Class Inheritance
Annotations
@SuppressWarnings:serial
Imports
java.text.DateFormat, .FieldPosition, .ParseException, .ParsePosition, .SimpleDateFormat, java.util.*, java.util.regex.Matcher, .Pattern, com.fasterxml.jackson.core.io.NumberInput

Jackson's internal DateFormat implementation used by standard Date serializers and deserializers to implement default behavior: does NOT fully implement all aspects expected by DateFormat and as a consequence SHOULD NOT to be used by code outside core Jackson databind functionality. In particular, ParsePosition argument of parse(String, ParsePosition) and format(Date, StringBuffer, FieldPosition) methods is fully ignored; Jackson itself never calls these methods.

For serialization defaults to using an ISO-8601 compliant format (format String "yyyy-MM-dd'T'HH:mm:ss.SSSZ") and for deserialization, both ISO-8601 and RFC-1123.
Note that `Z` in format String refers to ISO-8601 time offset notation which produces values like "-08:00" -- that is, full minute/hour combo without colon, and not using `Z` as alias for "+00:00". Inclusion of colon as separator, as default setting, started in Jackson 2.11: prior versions omitted it. Note that it is possible to enable/disable use of colon in time offset by using method withColonInTimeZone for creating new differently configured format instance, and configuring ObjectMapper with it.

Todo

in Jackson 2.14 or later, should change behavior to fail if ParsePosition is specified by caller (at least with non-0 offset).

Field Summary

Modifier and TypeField and Description
private transient Calendar
_calendar

Lazily instantiated calendar used by this instance for serialization (format(Date)).

private transient DateFormat
protected Boolean
_lenient

Explicit override for leniency, if specified.

protected final Locale
protected transient TimeZone
_timezone

Caller may want to explicitly override timezone to use; if so, we will have non-null value here.

private boolean
_tzSerializedWithColon

Whether the TZ offset must be formatted with a colon between hours and minutes (HH:mm format)

Defaults to true since 2.11: earlier versions defaulted to false for backwards compatibility reasons

protected static final String[]
ALL_FORMATS

For error messages we'll also need a list of all formats.

protected static final Calendar
CALENDAR

Blueprint "Calendar" instance for use during formatting.

protected static final DateFormat
public static final String
DATE_FORMAT_STR_ISO8601

Defines a commonly used date format that conforms to ISO-8601 date formatting standard, when it includes basic undecorated timezone definition.

protected static final String
DATE_FORMAT_STR_PLAIN

ISO-8601 with just the Date part, no time: needed for error messages

protected static final String
DATE_FORMAT_STR_RFC1123

This constant defines the date format specified by RFC 1123 / RFC 822.

protected static final Locale
protected static final TimeZone
DEFAULT_TIMEZONE

By default we use UTC for everything, with Jackson 2.7 and later (2.6 and earlier relied on GMT)

public static final StdDateFormat
instance

A singleton instance can be used for cloning purposes, as a blueprint of sorts.

protected static final Pattern
protected static final Pattern
protected static final String
Inherited from java.text.DateFormat:
AM_PM_FIELDcalendarDATE_FIELDDAY_OF_WEEK_FIELDDAY_OF_WEEK_IN_MONTH_FIELDDAY_OF_YEAR_FIELDDEFAULTERA_FIELDFULLHOUR0_FIELDHOUR1_FIELDHOUR_OF_DAY0_FIELDHOUR_OF_DAY1_FIELDLONGMEDIUMMILLISECOND_FIELDMINUTE_FIELDMONTH_FIELDnumberFormatSECOND_FIELDSHORTTIMEZONE_FIELDWEEK_OF_MONTH_FIELDWEEK_OF_YEAR_FIELDYEAR_FIELD

Constructor Summary

AccessConstructor and Description
public
public
StdDateFormat(TimeZone tz, Locale loc)
Deprecated

protected
protected
StdDateFormat(TimeZone tz, Locale loc, Boolean lenient, boolean formatTzOffsetWithColon)

Method Summary

Modifier and TypeMethod and Description
protected void
private static final DateFormat
_cloneFormat(DateFormat df, String format, TimeZone tz, Locale loc, Boolean lenient)

protected static <T> boolean
_equals(T value1, T value2)

protected void
_format(TimeZone tz, Locale loc, Date date, StringBuffer buffer)

protected void
_formatBCEYear(StringBuffer buffer, int bceYearNoSign)

protected Calendar
private static int
_parse2D(String str, int index)

private static int
_parse4D(String str, int index)

protected Date
protected Date
private Date
public StdDateFormat
clone()

Overrides java.text.DateFormat.clone.

Overrides Cloneable

public boolean
equals(Object
object to be compared for equality
o
)

Overrides java.text.DateFormat.equals.

Compares the specified object with this DateFormat for equality.

public StringBuffer
format(Date
a Date to be formatted into a date-time string.
date
,
StringBuffer
the string buffer for the returning date-time string.
toAppendTo
,
FieldPosition
keeps track on the position of the field within the returned string. For example, given a date-time text "1996.07.10 AD at 15:08:56 PDT", if the given fieldPosition is DateFormat#YEAR_FIELD, the begin index and end index of fieldPosition will be set to 0 and 4, respectively. Notice that if the same date-time field appears more than once in a pattern, the fieldPosition will be set for the first occurrence of that date-time field. For instance, formatting a Date to the date-time string "1 PM PDT (Pacific Daylight Time)" using the pattern "h a z (zzzz)" and the alignment field DateFormat#TIMEZONE_FIELD, the begin index and end index of fieldPosition will be set to 5 and 8, respectively, for the first occurrence of the timezone pattern character 'z'.
fieldPosition
)

Implements abstract java.text.DateFormat.format.

Formats a Date into a date-time string.

public static TimeZone
public static DateFormat
getISO8601Format(TimeZone tz, Locale loc)
Deprecated Since 2.9

Method for getting a non-shared DateFormat instance that uses specified timezone and can handle simple ISO-8601 compliant date format.

public static DateFormat
getRFC1123Format(TimeZone tz, Locale loc)
Deprecated Since 2.9

Method for getting a non-shared DateFormat instance that uses specific timezone and can handle RFC-1123 compliant date format.

public TimeZone
getTimeZone()

Overrides java.text.DateFormat.getTimeZone.

Gets the time zone.

public int
hashCode()

Overrides java.text.DateFormat.hashCode.

Returns the hash code for this DateFormat.

public boolean

Returns:

true if a colon is to be inserted between the hours and minutes of the TZ offset when serializing as String; otherwise false
isColonIncludedInTimeZone
()

Accessor for checking whether this instance would include colon within timezone serialization or not: if {code true}, timezone offset is serialized like -06:00; if {code false} as -0600.

public boolean
isLenient()

Overrides java.text.DateFormat.isLenient.

Tell whether date/time parsing is to be lenient.

protected boolean
looksLikeISO8601(String dateStr)

Helper method used to figure out if input looks like valid ISO-8601 string.

private static void
pad2(StringBuffer buffer, int value)

private static void
pad3(StringBuffer buffer, int value)

private static void
pad4(StringBuffer buffer, int value)

public Date
parse(String
A String whose beginning should be parsed.
dateStr
)

Overrides java.text.DateFormat.parse.

Parses text from the beginning of the given string to produce a date.

public Date
parse(String
The date/time string to be parsed
dateStr
,
ParsePosition
On input, the position at which to start parsing; on output, the position at which parsing terminated, or the start position if the parse failed.
pos
)

Implements abstract java.text.DateFormat.parse.

Parse a date/time string according to the given parse position.

protected Date
protected Date
public void
setLenient(boolean
when true, parsing is lenient
enabled
)

Overrides java.text.DateFormat.setLenient.

Need to override since we need to keep track of leniency locally, and not via underlying Calendar instance like base class does.

public void
setTimeZone(TimeZone
the given new time zone.
tz
)

Overrides java.text.DateFormat.setTimeZone.

Sets the time zone for the calendar of this DateFormat object.

public String
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

public StdDateFormat
withColonInTimeZone(boolean b)

"Mutant factory" method that will return an instance that has specified handling of colon when serializing timezone (timezone either written like +0500 or +05:00): either this instance (if setting would not change), or newly constructed instance with desired setting for colon inclusion.

public StdDateFormat
withLenient(Boolean b)

"Mutant factory" method that will return an instance that has specified leniency setting: either this instance (if setting would not change), or newly constructed instance.

public StdDateFormat
withLocale(Locale loc)

"Mutant factory" method that will return an instance that uses specified Locale: either this instance (if setting would not change), or newly constructed instance with different Locale to use.

public StdDateFormat
withTimeZone(TimeZone tz)

Method used for creating a new instance with specified timezone; if no timezone specified, defaults to the default timezone (UTC).

Inherited from java.text.DateFormat:
formatformatgetAvailableLocalesgetCalendargetDateInstancegetDateInstancegetDateInstancegetDateTimeInstancegetDateTimeInstancegetDateTimeInstancegetInstancegetNumberFormatgetTimeInstancegetTimeInstancegetTimeInstanceparseObjectsetCalendarsetNumberFormat

Field Detail

_calendarback to summary
private transient Calendar _calendar

Lazily instantiated calendar used by this instance for serialization (format(Date)).

Since
2.9.1
_formatRFC1123back to summary
private transient DateFormat _formatRFC1123
_lenientback to summary
protected Boolean _lenient

Explicit override for leniency, if specified.

Cannot be `final` because setLenient(boolean) returns `void`.

Since
2.7
_localeback to summary
protected final Locale _locale
_timezoneback to summary
protected transient TimeZone _timezone

Caller may want to explicitly override timezone to use; if so, we will have non-null value here.

_tzSerializedWithColonback to summary
private boolean _tzSerializedWithColon

Whether the TZ offset must be formatted with a colon between hours and minutes (HH:mm format)

Defaults to true since 2.11: earlier versions defaulted to false for backwards compatibility reasons

Since
2.9.1
ALL_FORMATSback to summary
protected static final String[] ALL_FORMATS

For error messages we'll also need a list of all formats.

CALENDARback to summary
protected static final Calendar CALENDAR

Blueprint "Calendar" instance for use during formatting. Cannot be used as is, due to thread-safety issues, but can be used for constructing actual instances more cheaply by cloning.

Since
2.9.1
DATE_FORMAT_RFC1123back to summary
protected static final DateFormat DATE_FORMAT_RFC1123
DATE_FORMAT_STR_ISO8601back to summary
public static final String DATE_FORMAT_STR_ISO8601

Defines a commonly used date format that conforms to ISO-8601 date formatting standard, when it includes basic undecorated timezone definition.

DATE_FORMAT_STR_PLAINback to summary
protected static final String DATE_FORMAT_STR_PLAIN

ISO-8601 with just the Date part, no time: needed for error messages

DATE_FORMAT_STR_RFC1123back to summary
protected static final String DATE_FORMAT_STR_RFC1123

This constant defines the date format specified by RFC 1123 / RFC 822. Used for parsing via `SimpleDateFormat` as well as error messages.

DEFAULT_LOCALEback to summary
protected static final Locale DEFAULT_LOCALE
DEFAULT_TIMEZONEback to summary
protected static final TimeZone DEFAULT_TIMEZONE

By default we use UTC for everything, with Jackson 2.7 and later (2.6 and earlier relied on GMT)

instanceback to summary
public static final StdDateFormat instance

A singleton instance can be used for cloning purposes, as a blueprint of sorts.

PATTERN_ISO8601back to summary
protected static final Pattern PATTERN_ISO8601
PATTERN_PLAINback to summary
protected static final Pattern PATTERN_PLAIN
PATTERN_PLAIN_STRback to summary
protected static final String PATTERN_PLAIN_STR

Constructor Detail

StdDateFormatback to summary
public StdDateFormat()
StdDateFormatback to summary
public StdDateFormat(TimeZone tz, Locale loc)

Deprecated

Annotations
@Deprecated
StdDateFormatback to summary
protected StdDateFormat(TimeZone tz, Locale loc, Boolean lenient)
StdDateFormatback to summary
protected StdDateFormat(TimeZone tz, Locale loc, Boolean lenient, boolean formatTzOffsetWithColon)
Since
2.9.1

Method Detail

_clearFormatsback to summary
protected void _clearFormats()
_cloneFormatback to summary
private static final DateFormat _cloneFormat(DateFormat df, String format, TimeZone tz, Locale loc, Boolean lenient)
_equalsback to summary
protected static <T> boolean _equals(T value1, T value2)
_formatback to summary
protected void _format(TimeZone tz, Locale loc, Date date, StringBuffer buffer)
_formatBCEYearback to summary
protected void _formatBCEYear(StringBuffer buffer, int bceYearNoSign)
_getCalendarback to summary
protected Calendar _getCalendar(TimeZone tz)
_parse2Dback to summary
private static int _parse2D(String str, int index)
_parse4Dback to summary
private static int _parse4D(String str, int index)
_parseAsISO8601back to summary
protected Date _parseAsISO8601(String dateStr, ParsePosition bogus) throws IllegalArgumentException, ParseException
_parseDateback to summary
protected Date _parseDate(String dateStr, ParsePosition pos) throws ParseException
_parseDateFromLongback to summary
private Date _parseDateFromLong(String longStr, ParsePosition pos) throws ParseException
cloneback to summary
public StdDateFormat clone()

Overrides java.text.DateFormat.clone.

Doc from java.text.DateFormat.clone.

Overrides Cloneable

Returns:StdDateFormat

Doc from java.text.Format.clone.

a clone of this instance.

Annotations
@Override
equalsback to summary
public boolean equals(Object o)

Overrides java.text.DateFormat.equals.

Doc from java.text.DateFormat.equals.

Compares the specified object with this DateFormat for equality. Returns true if the object is also a DateFormat and the two formats would format any value the same.

Parameters
o:Object

object to be compared for equality

Returns:boolean

true if the specified object is equal to this DateFormat

Annotations
@Override
formatback to summary
public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition)

Implements abstract java.text.DateFormat.format.

Doc from java.text.DateFormat.format.

Formats a Date into a date-time string. The formatted string is appended to the given StringBuffer.

Parameters
date:Date

a Date to be formatted into a date-time string.

toAppendTo:StringBuffer

the string buffer for the returning date-time string.

fieldPosition:FieldPosition

keeps track on the position of the field within the returned string. For example, given a date-time text "1996.07.10 AD at 15:08:56 PDT", if the given fieldPosition is DateFormat#YEAR_FIELD, the begin index and end index of fieldPosition will be set to 0 and 4, respectively. Notice that if the same date-time field appears more than once in a pattern, the fieldPosition will be set for the first occurrence of that date-time field. For instance, formatting a Date to the date-time string "1 PM PDT (Pacific Daylight Time)" using the pattern "h a z (zzzz)" and the alignment field DateFormat#TIMEZONE_FIELD, the begin index and end index of fieldPosition will be set to 5 and 8, respectively, for the first occurrence of the timezone pattern character 'z'.

Returns:StringBuffer

the string buffer passed in as toAppendTo, with formatted text appended.

Annotations
@Override
getDefaultTimeZoneback to summary
public static TimeZone getDefaultTimeZone()
getISO8601Formatback to summary
public static DateFormat getISO8601Format(TimeZone tz, Locale loc)

Deprecated

Since 2.9

Method for getting a non-shared DateFormat instance that uses specified timezone and can handle simple ISO-8601 compliant date format.

Annotations
@Deprecated
Since
2.4
getRFC1123Formatback to summary
public static DateFormat getRFC1123Format(TimeZone tz, Locale loc)

Deprecated

Since 2.9

Method for getting a non-shared DateFormat instance that uses specific timezone and can handle RFC-1123 compliant date format.

Annotations
@Deprecated
Since
2.4
getTimeZoneback to summary
public TimeZone getTimeZone()

Overrides java.text.DateFormat.getTimeZone.

Doc from java.text.DateFormat.getTimeZone.

Gets the time zone. This method is equivalent to the following call.

getCalendar().getTimeZone();
getCalendar().getTimeZone();
Returns:TimeZone

the time zone associated with the calendar of DateFormat.

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.text.DateFormat.hashCode.

Doc from java.text.DateFormat.hashCode.

Returns the hash code for this DateFormat.

Returns:int

the hash code for this DateFormat

Annotations
@Override
isColonIncludedInTimeZoneback to summary
public boolean isColonIncludedInTimeZone()

Accessor for checking whether this instance would include colon within timezone serialization or not: if {code true}, timezone offset is serialized like -06:00; if {code false} as -0600.

Note

only relevant for serialization (formatting), as deserialization (parsing) always accepts optional colon but does not require it, regardless of this setting.

Returns:boolean

true if a colon is to be inserted between the hours and minutes of the TZ offset when serializing as String; otherwise false

Since
2.9.1
isLenientback to summary
public boolean isLenient()

Overrides java.text.DateFormat.isLenient.

Doc from java.text.DateFormat.isLenient.

Tell whether date/time parsing is to be lenient. This method is equivalent to the following call.

getCalendar().isLenient();
getCalendar().isLenient();
Returns:boolean

true if the calendar is lenient; false otherwise.

Annotations
@Override
looksLikeISO8601back to summary
protected boolean looksLikeISO8601(String dateStr)

Helper method used to figure out if input looks like valid ISO-8601 string.

pad2back to summary
private static void pad2(StringBuffer buffer, int value)
pad3back to summary
private static void pad3(StringBuffer buffer, int value)
pad4back to summary
private static void pad4(StringBuffer buffer, int value)
parseback to summary
public Date parse(String dateStr) throws ParseException

Overrides java.text.DateFormat.parse.

Doc from java.text.DateFormat.parse.

Parses text from the beginning of the given string to produce a date. The method may not use the entire text of the given string.

See the parse(String, ParsePosition) method for more information on date parsing.

Parameters
dateStr:String

A String whose beginning should be parsed.

Returns:Date

A Date parsed from the string.

Annotations
@Override
Exceptions
ParseException:
if the beginning of the specified string cannot be parsed.
parseback to summary
public Date parse(String dateStr, ParsePosition pos)

Implements abstract java.text.DateFormat.parse.

Doc from java.text.DateFormat.parse.

Parse a date/time string according to the given parse position. For example, if this has the pattern "M/d/yy h:m a, z", then a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date that is equivalent to Date(837039900000L).

By default, parsing is lenient: If the input is not in the form used by this object's format method but can still be parsed as a date, then the parse succeeds. Clients may insist on strict adherence to the format by calling setLenient(false).

This parsing operation uses the calendar to produce a Date. As a result, the calendar's date-time fields and the TimeZone value may have been overwritten, depending on subclass implementations. Any TimeZone value that has previously been set by a call to setTimeZone may need to be restored for further operations.

Parameters
dateStr:String

The date/time string to be parsed

pos:ParsePosition

On input, the position at which to start parsing; on output, the position at which parsing terminated, or the start position if the parse failed.

Returns:Date

A Date, or null if the input could not be parsed

Annotations
@Override
parseAsISO8601back to summary
protected Date parseAsISO8601(String dateStr, ParsePosition pos) throws ParseException
parseAsRFC1123back to summary
protected Date parseAsRFC1123(String dateStr, ParsePosition pos)
setLenientback to summary
public void setLenient(boolean enabled)

Overrides java.text.DateFormat.setLenient.

Need to override since we need to keep track of leniency locally, and not via underlying Calendar instance like base class does.

Parameters
enabled:boolean

Doc from java.text.DateFormat.setLenient.

when true, parsing is lenient

Annotations
@Override
setTimeZoneback to summary
public void setTimeZone(TimeZone tz)

Overrides java.text.DateFormat.setTimeZone.

Doc from java.text.DateFormat.setTimeZone.

Sets the time zone for the calendar of this DateFormat object. This method is equivalent to the following call.

getCalendar().setTimeZone(zone);
getCalendar().setTimeZone(zone);

The TimeZone set by this method is overwritten by a setCalendar call.

The TimeZone set by this method may be overwritten as a result of a call to the parse method.

Parameters
tz:TimeZone

the given new time zone.

Annotations
@Override
toPatternback to summary
public String toPattern()
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override
withColonInTimeZoneback to summary
public StdDateFormat withColonInTimeZone(boolean b)

"Mutant factory" method that will return an instance that has specified handling of colon when serializing timezone (timezone either written like +0500 or +05:00): either this instance (if setting would not change), or newly constructed instance with desired setting for colon inclusion.

Note

does NOT affect deserialization as colon is optional accepted but not required -- put another way, either serialization is accepted by this class.

Since
2.9.1
withLenientback to summary
public StdDateFormat withLenient(Boolean b)

"Mutant factory" method that will return an instance that has specified leniency setting: either this instance (if setting would not change), or newly constructed instance.

Since
2.9
withLocaleback to summary
public StdDateFormat withLocale(Locale loc)

"Mutant factory" method that will return an instance that uses specified Locale: either this instance (if setting would not change), or newly constructed instance with different Locale to use.

withTimeZoneback to summary
public StdDateFormat withTimeZone(TimeZone tz)

Method used for creating a new instance with specified timezone; if no timezone specified, defaults to the default timezone (UTC).