public class DateConverter extends AbstractSingleValueConverter implements ErrorReporter
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.
Constructor and 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.
|
Modifier and Type | Method and Description |
---|---|
void |
appendErrors(ErrorWriter errorWriter)
Append context information to an
ErrorWriter . |
boolean |
canConvert(java.lang.Class type)
Determines whether the converter can marshall a particular type.
|
java.lang.Object |
fromString(java.lang.String str)
Unmarshals an Object from its single value representation.
|
java.lang.String |
toString(java.lang.Object obj)
Marshals an Object into a single value representation.
|
public DateConverter()
public DateConverter(java.util.TimeZone timeZone)
timeZone
- the TimeZone used to serialize the Datepublic DateConverter(boolean lenient)
lenient
- the lenient setting of DateFormat.setLenient(boolean)
public DateConverter(java.lang.String defaultFormat, java.lang.String[] acceptableFormats)
defaultFormat
- the default formatacceptableFormats
- fallback formatspublic DateConverter(java.lang.String defaultFormat, java.lang.String[] acceptableFormats, java.util.TimeZone timeZone)
defaultFormat
- the default formatacceptableFormats
- fallback formatspublic DateConverter(java.lang.String defaultFormat, java.lang.String[] acceptableFormats, boolean lenient)
defaultFormat
- the default formatacceptableFormats
- fallback formatslenient
- the lenient setting of DateFormat.setLenient(boolean)
public DateConverter(java.lang.String defaultFormat, java.lang.String[] acceptableFormats, java.util.TimeZone timeZone, boolean lenient)
defaultFormat
- the default formatacceptableFormats
- fallback formatstimeZone
- the TimeZone used to serialize the Datelenient
- the lenient setting of DateFormat.setLenient(boolean)
public DateConverter(java.lang.String defaultEraFormat, java.lang.String defaultFormat, java.lang.String[] acceptableFormats, java.util.Locale locale, java.util.TimeZone timeZone, boolean lenient)
defaultEraFormat
- the default format for dates in a different era (may be
null
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 of DateFormat.setLenient(boolean)
public boolean canConvert(java.lang.Class type)
ConverterMatcher
canConvert
in interface ConverterMatcher
canConvert
in class AbstractSingleValueConverter
type
- the Class representing the object type to be convertedpublic java.lang.Object fromString(java.lang.String str)
SingleValueConverter
fromString
in interface SingleValueConverter
fromString
in class AbstractSingleValueConverter
str
- the String with the single value of the Objectpublic java.lang.String toString(java.lang.Object obj)
SingleValueConverter
toString
in interface SingleValueConverter
toString
in class AbstractSingleValueConverter
obj
- the Object to be convertednull
public void appendErrors(ErrorWriter errorWriter)
ErrorReporter
ErrorWriter
.appendErrors
in interface ErrorReporter
errorWriter
- the error writerCopyright © 2004–2022 XStream. All rights reserved.