Class DateConverter
java.lang.Object
com.thoughtworks.xstream.converters.basic.AbstractSingleValueConverter
com.thoughtworks.xstream.converters.basic.DateConverter
- All Implemented Interfaces:
ConverterMatcher
,ErrorReporter
,SingleValueConverter
Converts a
Date
to a string as a date format, retaining precision down to milliseconds.
The formatted string is by default in UTC and English locale. You can provide a different Locale
and
TimeZone
that are used for serialization or null
to use always the current TimeZone. Note, that
the default format uses 3-letter time zones that can be ambiguous and may cause wrong results at deserialization and
is localized since Java 6.
Using a Java 7 runtime or higher, the converter supports the datetime format defined by W3C (a subset of ISO 8601) at deserialization. Only the formats that also contain the time information.
Dates in a different era are using a special default pattern that contains the era itself.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final ThreadSafeSimpleDateFormat[]
private static final String[]
private static final String
private static final String
private final ThreadSafeSimpleDateFormat
private final ThreadSafeSimpleDateFormat
private static final long
private static final TimeZone
-
Constructor Summary
ConstructorsConstructorDescriptionConstruct a DateConverter with standard formats and lenient set off.DateConverter
(boolean lenient) Construct a DateConverter with standard formats and using UTC.DateConverter
(String defaultFormat, String[] acceptableFormats) Construct a DateConverter with lenient set off using UTC.DateConverter
(String defaultFormat, String[] acceptableFormats, boolean lenient) Construct a DateConverter.DateConverter
(String defaultFormat, String[] acceptableFormats, TimeZone timeZone) Construct a DateConverter with a given TimeZone and lenient set off.DateConverter
(String defaultFormat, String[] acceptableFormats, TimeZone timeZone, boolean lenient) Construct a DateConverter.DateConverter
(String defaultEraFormat, String defaultFormat, String[] acceptableFormats, Locale locale, TimeZone timeZone, boolean lenient) Construct a DateConverter.DateConverter
(TimeZone timeZone) Construct a DateConverter with standard formats, lenient set off and uses a given TimeZone for serialization. -
Method Summary
Modifier and TypeMethodDescriptionvoid
appendErrors
(ErrorWriter errorWriter) Append context information to anErrorWriter
.boolean
canConvert
(Class type) Determines whether the converter can marshall a particular type.fromString
(String str) Unmarshals an Object from its single value representation.Marshals an Object into a single value representation.
-
Field Details
-
DEFAULT_ACCEPTABLE_FORMATS
-
DEFAULT_PATTERN
-
DEFAULT_ERA_PATTERN
-
UTC
-
ERA_START
private static final long ERA_START -
defaultFormat
-
defaultEraFormat
-
acceptableFormats
-
-
Constructor Details
-
DateConverter
public DateConverter()Construct a DateConverter with standard formats and lenient set off. -
DateConverter
Construct a DateConverter with standard formats, lenient set off and uses a given TimeZone for serialization.- Parameters:
timeZone
- the TimeZone used to serialize the Date- Since:
- 1.4
-
DateConverter
public DateConverter(boolean lenient) Construct a DateConverter with standard formats and using UTC.- Parameters:
lenient
- the lenient setting ofDateFormat.setLenient(boolean)
- Since:
- 1.3
-
DateConverter
Construct a DateConverter with lenient set off using UTC.- Parameters:
defaultFormat
- the default formatacceptableFormats
- fallback formats
-
DateConverter
Construct a DateConverter with a given TimeZone and lenient set off.- Parameters:
defaultFormat
- the default formatacceptableFormats
- fallback formats- Since:
- 1.4
-
DateConverter
Construct a DateConverter.- Parameters:
defaultFormat
- the default formatacceptableFormats
- fallback formatslenient
- the lenient setting ofDateFormat.setLenient(boolean)
- Since:
- 1.3
-
DateConverter
public DateConverter(String defaultFormat, String[] acceptableFormats, TimeZone timeZone, boolean lenient) Construct a DateConverter.- Parameters:
defaultFormat
- the default formatacceptableFormats
- fallback formatstimeZone
- the TimeZone used to serialize the Datelenient
- the lenient setting ofDateFormat.setLenient(boolean)
- Since:
- 1.4
-
DateConverter
public DateConverter(String defaultEraFormat, String defaultFormat, String[] acceptableFormats, Locale locale, TimeZone timeZone, boolean lenient) Construct a DateConverter.- Parameters:
defaultEraFormat
- the default format for dates in a different era (may benull
to drop era support)defaultFormat
- the default formatacceptableFormats
- fallback formatslocale
- locale to use for the formattimeZone
- the TimeZone used to serialize the Datelenient
- the lenient setting ofDateFormat.setLenient(boolean)
- Since:
- 1.4.4
-
-
Method Details
-
canConvert
Description copied from interface:ConverterMatcher
Determines whether the converter can marshall a particular type.- Specified by:
canConvert
in interfaceConverterMatcher
- Specified by:
canConvert
in classAbstractSingleValueConverter
- Parameters:
type
- the Class representing the object type to be converted
-
fromString
Description copied from interface:SingleValueConverter
Unmarshals an Object from its single value representation.- Specified by:
fromString
in interfaceSingleValueConverter
- Specified by:
fromString
in classAbstractSingleValueConverter
- Parameters:
str
- the String with the single value of the Object- Returns:
- the Object
-
toString
Description copied from interface:SingleValueConverter
Marshals an Object into a single value representation.- Specified by:
toString
in interfaceSingleValueConverter
- Overrides:
toString
in classAbstractSingleValueConverter
- Parameters:
obj
- the Object to be converted- Returns:
- a String with the single value of the Object or
null
-
appendErrors
Description copied from interface:ErrorReporter
Append context information to anErrorWriter
.- Specified by:
appendErrors
in interfaceErrorReporter
- Parameters:
errorWriter
- the error writer
-