Top Description Fields Constructors Methods
com.univocity.parsers.conversions

public Class DateConversion

extends ObjectConversion<Date>
implements FormattedConversion<SimpleDateFormat>
Class Inheritance
All Implemented Interfaces
com.univocity.parsers.conversions.FormattedConversion
Imports
com.univocity.parsers.common.*, java.text.*, java.util.*

Converts Strings to instances of java.util.Date and vice versa.

This class supports multiple date formats. For example, you can define conversions from dates represented by different Strings such as "2001/05/02 and Dec/2013".

The reverse conversion from a Date to String (in DateConversion#revert(Date) will return a formatted String using the date pattern provided in this class constructor

The date patterns must follows the pattern rules of java.text.SimpleDateFormat

Author
Univocity Software Pty Ltd - [email protected]
See Also
java.text.SimpleDateFormat

Field Summary

Modifier and TypeField and Description
private final String[]
private final Locale
private final SimpleDateFormat[]
private final TimeZone

Constructor Summary

AccessConstructor and Description
public
DateConversion(TimeZone
the TimeZone of the date to be formatted
timeZone
,
Locale
the Locale that determines how the date mask should be formatted.
locale
,
Date
default Date value to be returned when the input String is null. Used when ObjectConversion#execute(String) is invoked.
valueIfStringIsNull
,
String
default String value to be returned when a Date input is null. Used when DateConversion#revert(Date) is invoked.
valueIfObjectIsNull
,
String...
list of acceptable date patterns The first pattern in this sequence will be used to convert a Date into a String in DateConversion#revert(Date).
dateFormats
)

Defines a conversion from String to java.util.Date using a sequence of acceptable date patterns.

public
DateConversion(Locale
the Locale that determines how the date mask should be formatted.
locale
,
Date
default Date value to be returned when the input String is null. Used when ObjectConversion#execute(String) is invoked.
valueIfStringIsNull
,
String
default String value to be returned when a Date input is null. Used when DateConversion#revert(Date) is invoked.
valueIfObjectIsNull
,
String...
list of acceptable date patterns The first pattern in this sequence will be used to convert a Date into a String in DateConversion#revert(Date).
dateFormats
)

Defines a conversion from String to java.util.Date using a sequence of acceptable date patterns.

public
DateConversion(Date
default Date value to be returned when the input String is null. Used when ObjectConversion#execute(String) is invoked.
valueIfStringIsNull
,
String
default String value to be returned when a Date input is null. Used when DateConversion#revert(Date) is invoked.
valueIfObjectIsNull
,
String...
list of acceptable date patterns The first pattern in this sequence will be used to convert a Date into a String in DateConversion#revert(Date).
dateFormats
)

Defines a conversion from String to java.util.Date using a sequence of acceptable date patterns.

public
DateConversion(Locale
the Locale that determines how the date mask should be formatted.
locale
,
String...
list of acceptable date patterns The first pattern in this sequence will be used to convert a Date into a String in DateConversion#revert(Date).
dateFormats
)

Defines a conversion from String to java.util.Date using a sequence of acceptable date patterns.

public
DateConversion(String...
list of acceptable date patterns The first pattern in this sequence will be used to convert a Date into a String in DateConversion#revert(Date).
dateFormats
)

Defines a conversion from String to java.util.Date using a sequence of acceptable date patterns.

Method Summary

Modifier and TypeMethod and Description
protected Date

Returns:

the Date instance containing the date information represented by the given String, or the value of valueIfObjectIsNull if the String input is null.
fromString
(String
the String containing a formatted date which must be converted to a Date
input
)

Implements abstract com.univocity.parsers.conversions.ObjectConversion.fromString.

Converts a formatted date String to an instance of Date.
public SimpleDateFormat[]
public TimeZone
public String

Returns:

a formatted date String representing the date provided by the given Date, or the value of valueIfObjectIsNull if the Date parameter is null.
revert
(Date
the Date to be converted to a String
input
)

Overrides com.univocity.parsers.conversions.ObjectConversion.revert.

Implements com.univocity.parsers.conversions.Conversion.revert.

Converts Date to a formatted date String.
Inherited from com.univocity.parsers.conversions.ObjectConversion:
executefromInputgetValueIfObjectIsNullgetValueIfStringIsNullsetValueIfObjectIsNullsetValueIfStringIsNullundo

Field Detail

formatsback to summary
private final String[] formats
localeback to summary
private final Locale locale
parsersback to summary
private final SimpleDateFormat[] parsers
timeZoneback to summary
private final TimeZone timeZone

Constructor Detail

DateConversionback to summary
public DateConversion(TimeZone timeZone, Locale locale, Date valueIfStringIsNull, String valueIfObjectIsNull, String... dateFormats)

Defines a conversion from String to java.util.Date using a sequence of acceptable date patterns. This constructor assumes the output of a conversion should be null when input is null

Parameters
timeZone:TimeZone

the TimeZone of the date to be formatted

locale:Locale

the Locale that determines how the date mask should be formatted.

valueIfStringIsNull:Date

default Date value to be returned when the input String is null. Used when ObjectConversion#execute(String) is invoked.

valueIfObjectIsNull:String

default String value to be returned when a Date input is null. Used when DateConversion#revert(Date) is invoked.

dateFormats:String[]

list of acceptable date patterns The first pattern in this sequence will be used to convert a Date into a String in DateConversion#revert(Date).

DateConversionback to summary
public DateConversion(Locale locale, Date valueIfStringIsNull, String valueIfObjectIsNull, String... dateFormats)

Defines a conversion from String to java.util.Date using a sequence of acceptable date patterns. This constructor assumes the output of a conversion should be null when input is null

Parameters
locale:Locale

the Locale that determines how the date mask should be formatted.

valueIfStringIsNull:Date

default Date value to be returned when the input String is null. Used when ObjectConversion#execute(String) is invoked.

valueIfObjectIsNull:String

default String value to be returned when a Date input is null. Used when DateConversion#revert(Date) is invoked.

dateFormats:String[]

list of acceptable date patterns The first pattern in this sequence will be used to convert a Date into a String in DateConversion#revert(Date).

DateConversionback to summary
public DateConversion(Date valueIfStringIsNull, String valueIfObjectIsNull, String... dateFormats)

Defines a conversion from String to java.util.Date using a sequence of acceptable date patterns. This constructor assumes the output of a conversion should be null when input is null

Parameters
valueIfStringIsNull:Date

default Date value to be returned when the input String is null. Used when ObjectConversion#execute(String) is invoked.

valueIfObjectIsNull:String

default String value to be returned when a Date input is null. Used when DateConversion#revert(Date) is invoked.

dateFormats:String[]

list of acceptable date patterns The first pattern in this sequence will be used to convert a Date into a String in DateConversion#revert(Date).

DateConversionback to summary
public DateConversion(Locale locale, String... dateFormats)

Defines a conversion from String to java.util.Date using a sequence of acceptable date patterns. This constructor assumes the output of a conversion should be null when input is null

Parameters
locale:Locale

the Locale that determines how the date mask should be formatted.

dateFormats:String[]

list of acceptable date patterns The first pattern in this sequence will be used to convert a Date into a String in DateConversion#revert(Date).

DateConversionback to summary
public DateConversion(String... dateFormats)

Defines a conversion from String to java.util.Date using a sequence of acceptable date patterns. This constructor assumes the output of a conversion should be null when input is null

Parameters
dateFormats:String[]

list of acceptable date patterns The first pattern in this sequence will be used to convert a Date into a String in DateConversion#revert(Date).

Method Detail

fromStringback to summary
protected Date fromString(String input)

Implements abstract com.univocity.parsers.conversions.ObjectConversion.fromString.

Converts a formatted date String to an instance of Date.

The pattern in the formatted date must match one of the date patterns provided in the constructor of this class.

Parameters
input:String

the String containing a formatted date which must be converted to a Date

Returns:Date

the Date instance containing the date information represented by the given String, or the value of valueIfObjectIsNull if the String input is null.

Annotations
@Override
getFormatterObjectsback to summary
public SimpleDateFormat[] getFormatterObjects()

Implements com.univocity.parsers.conversions.FormattedConversion.getFormatterObjects.

Doc from com.univocity.parsers.conversions.FormattedConversion.getFormatterObjects.

Returns the formatter objects

Returns:SimpleDateFormat[]

the formatter objects used to apply formatting to values to generate formatted Strings, and parsing formatted Strings into values

Annotations
@Override
getTimeZoneback to summary
public TimeZone getTimeZone()
revertback to summary
public String revert(Date input)

Overrides com.univocity.parsers.conversions.ObjectConversion.revert.

Implements com.univocity.parsers.conversions.Conversion.revert.

Converts Date to a formatted date String.

The pattern used to generate the formatted date is the first date pattern provided in the constructor of this class

Parameters
input:Date

the Date to be converted to a String

Returns:String

a formatted date String representing the date provided by the given Date, or the value of valueIfObjectIsNull if the Date parameter is null.

Annotations
@Override