Class DateConverter
- java.lang.Object
-
- com.thoughtworks.xstream.converters.basic.AbstractSingleValueConverter
-
- com.thoughtworks.xstream.converters.basic.DateConverter
-
- All Implemented Interfaces:
ConverterMatcher,ErrorReporter,SingleValueConverter
public class DateConverter extends AbstractSingleValueConverter implements ErrorReporter
Converts aDateto 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
LocaleandTimeZonethat are used for serialization ornullto 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
Fields Modifier and Type Field Description private ThreadSafeSimpleDateFormat[]acceptableFormatsprivate static java.lang.String[]DEFAULT_ACCEPTABLE_FORMATSprivate static java.lang.StringDEFAULT_ERA_PATTERNprivate static java.lang.StringDEFAULT_PATTERNprivate ThreadSafeSimpleDateFormatdefaultEraFormatprivate ThreadSafeSimpleDateFormatdefaultFormatprivate static longERA_STARTprivate static java.util.TimeZoneUTC
-
Constructor Summary
Constructors Constructor Description DateConverter()Construct a DateConverter with standard formats and lenient set off.DateConverter(boolean lenient)Construct a DateConverter with standard formats and using UTC.DateConverter(java.lang.String defaultFormat, java.lang.String[] acceptableFormats)Construct a DateConverter with lenient set off using UTC.DateConverter(java.lang.String defaultFormat, java.lang.String[] acceptableFormats, boolean lenient)Construct a DateConverter.DateConverter(java.lang.String defaultFormat, java.lang.String[] acceptableFormats, java.util.TimeZone timeZone)Construct a DateConverter with a given TimeZone and lenient set off.DateConverter(java.lang.String defaultFormat, java.lang.String[] acceptableFormats, java.util.TimeZone timeZone, boolean lenient)Construct a DateConverter.DateConverter(java.lang.String defaultEraFormat, java.lang.String defaultFormat, java.lang.String[] acceptableFormats, java.util.Locale locale, java.util.TimeZone timeZone, boolean lenient)Construct a DateConverter.DateConverter(java.util.TimeZone timeZone)Construct a DateConverter with standard formats, lenient set off and uses a given TimeZone for serialization.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidappendErrors(ErrorWriter errorWriter)Append context information to anErrorWriter.booleancanConvert(java.lang.Class type)Determines whether the converter can marshall a particular type.java.lang.ObjectfromString(java.lang.String str)Unmarshals an Object from its single value representation.java.lang.StringtoString(java.lang.Object obj)Marshals an Object into a single value representation.
-
-
-
Field Detail
-
DEFAULT_ACCEPTABLE_FORMATS
private static final java.lang.String[] DEFAULT_ACCEPTABLE_FORMATS
-
DEFAULT_PATTERN
private static final java.lang.String DEFAULT_PATTERN
-
DEFAULT_ERA_PATTERN
private static final java.lang.String DEFAULT_ERA_PATTERN
-
UTC
private static final java.util.TimeZone UTC
-
ERA_START
private static final long ERA_START
-
defaultFormat
private final ThreadSafeSimpleDateFormat defaultFormat
-
defaultEraFormat
private final ThreadSafeSimpleDateFormat defaultEraFormat
-
acceptableFormats
private final ThreadSafeSimpleDateFormat[] acceptableFormats
-
-
Constructor Detail
-
DateConverter
public DateConverter()
Construct a DateConverter with standard formats and lenient set off.
-
DateConverter
public DateConverter(java.util.TimeZone timeZone)
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
public DateConverter(java.lang.String defaultFormat, java.lang.String[] acceptableFormats)Construct a DateConverter with lenient set off using UTC.- Parameters:
defaultFormat- the default formatacceptableFormats- fallback formats
-
DateConverter
public DateConverter(java.lang.String defaultFormat, java.lang.String[] acceptableFormats, java.util.TimeZone timeZone)Construct a DateConverter with a given TimeZone and lenient set off.- Parameters:
defaultFormat- the default formatacceptableFormats- fallback formats- Since:
- 1.4
-
DateConverter
public DateConverter(java.lang.String defaultFormat, java.lang.String[] acceptableFormats, boolean lenient)Construct a DateConverter.- Parameters:
defaultFormat- the default formatacceptableFormats- fallback formatslenient- the lenient setting ofDateFormat.setLenient(boolean)- Since:
- 1.3
-
DateConverter
public DateConverter(java.lang.String defaultFormat, java.lang.String[] acceptableFormats, java.util.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(java.lang.String defaultEraFormat, java.lang.String defaultFormat, java.lang.String[] acceptableFormats, java.util.Locale locale, java.util.TimeZone timeZone, boolean lenient)Construct a DateConverter.- Parameters:
defaultEraFormat- the default format for dates in a different era (may benullto 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 Detail
-
canConvert
public boolean canConvert(java.lang.Class type)
Description copied from interface:ConverterMatcherDetermines whether the converter can marshall a particular type.- Specified by:
canConvertin interfaceConverterMatcher- Specified by:
canConvertin classAbstractSingleValueConverter- Parameters:
type- the Class representing the object type to be converted
-
fromString
public java.lang.Object fromString(java.lang.String str)
Description copied from interface:SingleValueConverterUnmarshals an Object from its single value representation.- Specified by:
fromStringin interfaceSingleValueConverter- Specified by:
fromStringin classAbstractSingleValueConverter- Parameters:
str- the String with the single value of the Object- Returns:
- the Object
-
toString
public java.lang.String toString(java.lang.Object obj)
Description copied from interface:SingleValueConverterMarshals an Object into a single value representation.- Specified by:
toStringin interfaceSingleValueConverter- Overrides:
toStringin classAbstractSingleValueConverter- Parameters:
obj- the Object to be converted- Returns:
- a String with the single value of the Object or
null
-
appendErrors
public void appendErrors(ErrorWriter errorWriter)
Description copied from interface:ErrorReporterAppend context information to anErrorWriter.- Specified by:
appendErrorsin interfaceErrorReporter- Parameters:
errorWriter- the error writer
-
-