Top Description Fields Constructors Methods
com.thoughtworks.xstream.converters.basic

public Class DateConverter

extends AbstractSingleValueConverter
implements ErrorReporter
Class Inheritance
All Implemented Interfaces
com.thoughtworks.xstream.converters.ErrorReporter
Imports
java.text.ParseException, .SimpleDateFormat, java.util.ArrayList, .Calendar, .Date, .List, .Locale, .TimeZone, com.thoughtworks.xstream.converters.ConversionException, .ErrorReporter, .ErrorWriter, com.thoughtworks.xstream.core.JVM, com.thoughtworks.xstream.core.util.ThreadSafeSimpleDateFormat

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.

Authors
Joe Walnes, Jörg Schaible

Field Summary

Modifier and TypeField and Description
private 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

AccessConstructor and Description
public
DateConverter()

Construct a DateConverter with standard formats and lenient set off.

public
DateConverter(final TimeZone
the TimeZone used to serialize the Date
timeZone
)

Construct a DateConverter with standard formats, lenient set off and uses a given TimeZone for serialization.

public
DateConverter(final boolean lenient)

Construct a DateConverter with standard formats and using UTC.

public
DateConverter(final String
the default format
defaultFormat
,
final String[]
fallback formats
acceptableFormats
)

Construct a DateConverter with lenient set off using UTC.

public
DateConverter(final String
the default format
defaultFormat
,
final String[]
fallback formats
acceptableFormats
,
final TimeZone timeZone)

Construct a DateConverter with a given TimeZone and lenient set off.

public
DateConverter(final String
the default format
defaultFormat
,
final String[]
fallback formats
acceptableFormats
,
final boolean lenient)

Construct a DateConverter.

public
DateConverter(final String
the default format
defaultFormat
,
final String[]
fallback formats
acceptableFormats
,
final TimeZone
the TimeZone used to serialize the Date
timeZone
,
final boolean lenient)

Construct a DateConverter.

public
DateConverter(final String
the default format for dates in a different era (may be null to drop era support)
defaultEraFormat
,
final String
the default format
defaultFormat
,
final String[]
fallback formats
acceptableFormats
,
final Locale
locale to use for the format
locale
,
final TimeZone
the TimeZone used to serialize the Date
timeZone
,
final boolean lenient)

Construct a DateConverter.

Method Summary

Modifier and TypeMethod and Description
public void
appendErrors(ErrorWriter
the error writer
errorWriter
)

Implements com.thoughtworks.xstream.converters.ErrorReporter.appendErrors.

Append context information to an ErrorWriter.
public boolean
canConvert(Class<T>
the Class representing the object type to be converted
type
)

Implements abstract com.thoughtworks.xstream.converters.basic.AbstractSingleValueConverter.canConvert.

Implements com.thoughtworks.xstream.converters.ConverterMatcher.canConvert.

Determines whether the converter can marshall a particular type.
public Object
fromString(String
the String with the single value of the Object
str
)

Implements abstract com.thoughtworks.xstream.converters.basic.AbstractSingleValueConverter.fromString.

Implements com.thoughtworks.xstream.converters.SingleValueConverter.fromString.

Unmarshals an Object from its single value representation.
public String
toString(Object
the Object to be converted
obj
)

Overrides com.thoughtworks.xstream.converters.basic.AbstractSingleValueConverter.toString.

Implements com.thoughtworks.xstream.converters.SingleValueConverter.toString.

Marshals an Object into a single value representation.

Field Detail

acceptableFormatsback to summary
private final ThreadSafeSimpleDateFormat[] acceptableFormats
DEFAULT_ACCEPTABLE_FORMATSback to summary
private static final String[] DEFAULT_ACCEPTABLE_FORMATS
DEFAULT_ERA_PATTERNback to summary
private static final String DEFAULT_ERA_PATTERN
DEFAULT_PATTERNback to summary
private static final String DEFAULT_PATTERN
defaultEraFormatback to summary
private final ThreadSafeSimpleDateFormat defaultEraFormat
defaultFormatback to summary
private final ThreadSafeSimpleDateFormat defaultFormat
ERA_STARTback to summary
private static final long ERA_START
UTCback to summary
private static final TimeZone UTC

Constructor Detail

DateConverterback to summary
public DateConverter()

Construct a DateConverter with standard formats and lenient set off.

DateConverterback to summary
public DateConverter(final TimeZone timeZone)

Construct a DateConverter with standard formats, lenient set off and uses a given TimeZone for serialization.

Parameters
timeZone:TimeZone

the TimeZone used to serialize the Date

Since
1.4
DateConverterback to summary
public DateConverter(final boolean lenient)

Construct a DateConverter with standard formats and using UTC.

Parameters
lenient:boolean

the lenient setting of SimpleDateFormat#setLenient(boolean)

Since
1.3
DateConverterback to summary
public DateConverter(final String defaultFormat, final String[] acceptableFormats)

Construct a DateConverter with lenient set off using UTC.

Parameters
defaultFormat:String

the default format

acceptableFormats:String[]

fallback formats

DateConverterback to summary
public DateConverter(final String defaultFormat, final String[] acceptableFormats, final TimeZone timeZone)

Construct a DateConverter with a given TimeZone and lenient set off.

Parameters
defaultFormat:String

the default format

acceptableFormats:String[]

fallback formats

Since
1.4
DateConverterback to summary
public DateConverter(final String defaultFormat, final String[] acceptableFormats, final boolean lenient)

Construct a DateConverter.

Parameters
defaultFormat:String

the default format

acceptableFormats:String[]

fallback formats

lenient:boolean

the lenient setting of SimpleDateFormat#setLenient(boolean)

Since
1.3
DateConverterback to summary
public DateConverter(final String defaultFormat, final String[] acceptableFormats, final TimeZone timeZone, final boolean lenient)

Construct a DateConverter.

Parameters
defaultFormat:String

the default format

acceptableFormats:String[]

fallback formats

timeZone:TimeZone

the TimeZone used to serialize the Date

lenient:boolean

the lenient setting of SimpleDateFormat#setLenient(boolean)

Since
1.4
DateConverterback to summary
public DateConverter(final String defaultEraFormat, final String defaultFormat, final String[] acceptableFormats, final Locale locale, final TimeZone timeZone, final boolean lenient)

Construct a DateConverter.

Parameters
defaultEraFormat:String

the default format for dates in a different era (may be null to drop era support)

defaultFormat:String

the default format

acceptableFormats:String[]

fallback formats

locale:Locale

locale to use for the format

timeZone:TimeZone

the TimeZone used to serialize the Date

lenient:boolean

the lenient setting of SimpleDateFormat#setLenient(boolean)

Since
1.4.4

Method Detail

appendErrorsback to summary
public void appendErrors(ErrorWriter errorWriter)

Implements com.thoughtworks.xstream.converters.ErrorReporter.appendErrors.

Doc from com.thoughtworks.xstream.converters.ErrorReporter.appendErrors.

Append context information to an ErrorWriter.

Parameters
errorWriter:ErrorWriter

the error writer

canConvertback to summary
public boolean canConvert(Class<T> type)

Implements abstract com.thoughtworks.xstream.converters.basic.AbstractSingleValueConverter.canConvert.

Implements com.thoughtworks.xstream.converters.ConverterMatcher.canConvert.

Doc from com.thoughtworks.xstream.converters.ConverterMatcher.canConvert.

Determines whether the converter can marshall a particular type.

Parameters
type:Class<T>

the Class representing the object type to be converted

fromStringback to summary
public Object fromString(String str)

Implements abstract com.thoughtworks.xstream.converters.basic.AbstractSingleValueConverter.fromString.

Implements com.thoughtworks.xstream.converters.SingleValueConverter.fromString.

Doc from com.thoughtworks.xstream.converters.SingleValueConverter.fromString.

Unmarshals an Object from its single value representation.

Parameters
str:String

the String with the single value of the Object

Returns:Object

the Object

toStringback to summary
public String toString(Object obj)

Overrides com.thoughtworks.xstream.converters.basic.AbstractSingleValueConverter.toString.

Implements com.thoughtworks.xstream.converters.SingleValueConverter.toString.

Doc from com.thoughtworks.xstream.converters.SingleValueConverter.toString.

Marshals an Object into a single value representation.

Parameters
obj:Object

the Object to be converted

Returns:String

a String with the single value of the Object or null