Class DateConversion
- java.lang.Object
-
- com.univocity.parsers.conversions.NullConversion<java.lang.String,T>
-
- com.univocity.parsers.conversions.ObjectConversion<java.util.Date>
-
- com.univocity.parsers.conversions.DateConversion
-
- All Implemented Interfaces:
Conversion<java.lang.String,java.util.Date>,FormattedConversion<java.text.SimpleDateFormat>
public class DateConversion extends ObjectConversion<java.util.Date> implements FormattedConversion<java.text.SimpleDateFormat>
Converts Strings to instances ofDateand 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
revert(Date)will return a formatted String using the date pattern provided in this class constructorThe date patterns must follows the pattern rules of
SimpleDateFormat- See Also:
SimpleDateFormat
-
-
Constructor Summary
Constructors Constructor Description DateConversion(java.lang.String... dateFormats)Defines a conversion from String toDateusing a sequence of acceptable date patterns.DateConversion(java.util.Date valueIfStringIsNull, java.lang.String valueIfObjectIsNull, java.lang.String... dateFormats)Defines a conversion from String toDateusing a sequence of acceptable date patterns.DateConversion(java.util.Locale locale, java.lang.String... dateFormats)Defines a conversion from String toDateusing a sequence of acceptable date patterns.DateConversion(java.util.Locale locale, java.util.Date valueIfStringIsNull, java.lang.String valueIfObjectIsNull, java.lang.String... dateFormats)Defines a conversion from String toDateusing a sequence of acceptable date patterns.DateConversion(java.util.TimeZone timeZone, java.util.Locale locale, java.util.Date valueIfStringIsNull, java.lang.String valueIfObjectIsNull, java.lang.String... dateFormats)Defines a conversion from String toDateusing a sequence of acceptable date patterns.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected java.util.DatefromString(java.lang.String input)Converts a formatted date String to an instance of Date.java.text.SimpleDateFormat[]getFormatterObjects()Returns the formatter objectsjava.util.TimeZonegetTimeZone()java.lang.Stringrevert(java.util.Date input)Converts Date to a formatted date String.-
Methods inherited from class com.univocity.parsers.conversions.ObjectConversion
execute, fromInput, getValueIfObjectIsNull, getValueIfStringIsNull, setValueIfObjectIsNull, setValueIfStringIsNull, undo
-
Methods inherited from class com.univocity.parsers.conversions.NullConversion
getValueOnNullInput, getValueOnNullOutput, setValueOnNullInput, setValueOnNullOutput
-
-
-
-
Constructor Detail
-
DateConversion
public DateConversion(java.util.TimeZone timeZone, java.util.Locale locale, java.util.Date valueIfStringIsNull, java.lang.String valueIfObjectIsNull, java.lang.String... dateFormats)Defines a conversion from String toDateusing a sequence of acceptable date patterns. This constructor assumes the output of a conversion should be null when input is null- Parameters:
timeZone- theTimeZoneof the date to be formattedlocale- theLocalethat determines how the date mask should be formatted.valueIfStringIsNull- default Date value to be returned when the input String is null. Used whenObjectConversion.execute(String)is invoked.valueIfObjectIsNull- default String value to be returned when a Date input is null. Used whenrevert(Date)is invoked.dateFormats- list of acceptable date patterns The first pattern in this sequence will be used to convert a Date into a String inrevert(Date).
-
DateConversion
public DateConversion(java.util.Locale locale, java.util.Date valueIfStringIsNull, java.lang.String valueIfObjectIsNull, java.lang.String... dateFormats)Defines a conversion from String toDateusing a sequence of acceptable date patterns. This constructor assumes the output of a conversion should be null when input is null- Parameters:
locale- theLocalethat determines how the date mask should be formatted.valueIfStringIsNull- default Date value to be returned when the input String is null. Used whenObjectConversion.execute(String)is invoked.valueIfObjectIsNull- default String value to be returned when a Date input is null. Used whenrevert(Date)is invoked.dateFormats- list of acceptable date patterns The first pattern in this sequence will be used to convert a Date into a String inrevert(Date).
-
DateConversion
public DateConversion(java.util.Date valueIfStringIsNull, java.lang.String valueIfObjectIsNull, java.lang.String... dateFormats)Defines a conversion from String toDateusing a sequence of acceptable date patterns. This constructor assumes the output of a conversion should be null when input is null- Parameters:
valueIfStringIsNull- default Date value to be returned when the input String is null. Used whenObjectConversion.execute(String)is invoked.valueIfObjectIsNull- default String value to be returned when a Date input is null. Used whenrevert(Date)is invoked.dateFormats- list of acceptable date patterns The first pattern in this sequence will be used to convert a Date into a String inrevert(Date).
-
DateConversion
public DateConversion(java.util.Locale locale, java.lang.String... dateFormats)Defines a conversion from String toDateusing a sequence of acceptable date patterns. This constructor assumes the output of a conversion should be null when input is null- Parameters:
locale- theLocalethat determines how the date mask should be formatted.dateFormats- list of acceptable date patterns The first pattern in this sequence will be used to convert a Date into a String inrevert(Date).
-
DateConversion
public DateConversion(java.lang.String... dateFormats)
Defines a conversion from String toDateusing a sequence of acceptable date patterns. This constructor assumes the output of a conversion should be null when input is null- Parameters:
dateFormats- list of acceptable date patterns The first pattern in this sequence will be used to convert a Date into a String inrevert(Date).
-
-
Method Detail
-
revert
public java.lang.String revert(java.util.Date input)
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
- Specified by:
revertin interfaceConversion<java.lang.String,java.util.Date>- Overrides:
revertin classObjectConversion<java.util.Date>- Parameters:
input- the Date to be converted to a String- Returns:
- a formatted date String representing the date provided by the given Date, or the value of
valueIfObjectIsNullif the Date parameter is null.
-
fromString
protected java.util.Date fromString(java.lang.String input)
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.
- Specified by:
fromStringin classObjectConversion<java.util.Date>- Parameters:
input- the String containing a formatted date which must be converted to a Date- Returns:
- the Date instance containing the date information represented by the given String, or the value of
valueIfObjectIsNullif the String input is null.
-
getFormatterObjects
public java.text.SimpleDateFormat[] getFormatterObjects()
Description copied from interface:FormattedConversionReturns the formatter objects- Specified by:
getFormatterObjectsin interfaceFormattedConversion<java.text.SimpleDateFormat>- Returns:
- the formatter objects used to apply formatting to values to generate formatted Strings, and parsing formatted Strings into values
-
getTimeZone
public java.util.TimeZone getTimeZone()
-
-