Package org.supercsv.cellprocessor
Class ParseDate
- java.lang.Object
-
- org.supercsv.cellprocessor.CellProcessorAdaptor
-
- org.supercsv.cellprocessor.ParseDate
-
- All Implemented Interfaces:
CellProcessor
,StringCellProcessor
public class ParseDate extends CellProcessorAdaptor implements StringCellProcessor
Converts a String to a Date using theSimpleDateFormat
class. If you want to convert from a Date to a String, use theFmtDate
processor.Some example date formats you can use are:
"dd/MM/yyyy"
(parses a date formatted as "25/12/2011")
"dd-MMM-yy"
(parses a date formatted as "25-Dec-11")
"yyyy.MM.dd.HH.mm.ss"
(parses a date formatted as "2011.12.25.08.36.33"
"E, dd MMM yyyy HH:mm:ss Z"
(parses a date formatted as "Tue, 25 Dec 2011 08:36:33 -0500")
This processor caters for lenient or non-lenient date interpretations (the default is false for constructors without a 'lenient' parameter). See
DateFormat.setLenient(boolean)
for more information.If you don't wish to use the default Locale when parsing Dates (your data is formatted for a different Locale), then use the constructor that accepts a Locale.
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.String
dateFormat
private boolean
lenient
private java.util.Locale
locale
-
Fields inherited from class org.supercsv.cellprocessor.CellProcessorAdaptor
next
-
-
Constructor Summary
Constructors Constructor Description ParseDate(java.lang.String dateFormat)
Constructs a new ParseDate processor which converts a String to a Date using the supplied date format.ParseDate(java.lang.String dateFormat, boolean lenient)
Constructs a new ParseDate processor which converts a String to a Date using the supplied date format.ParseDate(java.lang.String dateFormat, boolean lenient, java.util.Locale locale)
Constructs a new ParseDate processor which converts a String to a Date using the supplied date format and Locale.ParseDate(java.lang.String dateFormat, boolean lenient, java.util.Locale locale, DateCellProcessor next)
Constructs a new ParseDate processor which converts a String to a Date using the supplied date format and Locale, then calls the next processor in the chain.ParseDate(java.lang.String dateFormat, boolean lenient, DateCellProcessor next)
Constructs a new ParseDate processor which converts a String to a Date using the supplied date format, then calls the next processor in the chain.ParseDate(java.lang.String dateFormat, DateCellProcessor next)
Constructs a new ParseDate processor which converts a String to a Date using the supplied date format, then calls the next processor in the chain.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static void
checkPreconditions(java.lang.String dateFormat)
Checks the preconditions for creating a new ParseDate processor with a date format.private static void
checkPreconditions(java.lang.String dateFormat, java.util.Locale locale)
Checks the preconditions for creating a new ParseDate processor with date format and locale.java.lang.Object
execute(java.lang.Object value, CsvContext context)
This method is invoked by the framework when the processor needs to process data or check constraints.-
Methods inherited from class org.supercsv.cellprocessor.CellProcessorAdaptor
toString, validateInputNotNull
-
-
-
-
Constructor Detail
-
ParseDate
public ParseDate(java.lang.String dateFormat)
Constructs a new ParseDate processor which converts a String to a Date using the supplied date format. This constructor uses non-lenient Date interpretation.- Parameters:
dateFormat
- the date format to use- Throws:
java.lang.NullPointerException
- if dateFormat is null
-
ParseDate
public ParseDate(java.lang.String dateFormat, boolean lenient)
Constructs a new ParseDate processor which converts a String to a Date using the supplied date format.- Parameters:
dateFormat
- the date format to uselenient
- whether date interpretation is lenient- Throws:
java.lang.NullPointerException
- if dateFormat is null
-
ParseDate
public ParseDate(java.lang.String dateFormat, boolean lenient, java.util.Locale locale)
Constructs a new ParseDate processor which converts a String to a Date using the supplied date format and Locale.- Parameters:
dateFormat
- the date format to uselenient
- whether date interpretation is lenientlocale
- the Locale used to parse the date- Throws:
java.lang.NullPointerException
- if dateFormat or locale is null
-
ParseDate
public ParseDate(java.lang.String dateFormat, DateCellProcessor next)
Constructs a new ParseDate processor which converts a String to a Date using the supplied date format, then calls the next processor in the chain. This constructor uses non-lenient Date interpretation.- Parameters:
dateFormat
- the date format to usenext
- the next processor in the chain- Throws:
java.lang.NullPointerException
- if dateFormat or next is null
-
ParseDate
public ParseDate(java.lang.String dateFormat, boolean lenient, DateCellProcessor next)
Constructs a new ParseDate processor which converts a String to a Date using the supplied date format, then calls the next processor in the chain.- Parameters:
dateFormat
- the date format to uselenient
- whether date interpretation is lenientnext
- the next processor in the chain- Throws:
java.lang.NullPointerException
- if dateFormat or next is null
-
ParseDate
public ParseDate(java.lang.String dateFormat, boolean lenient, java.util.Locale locale, DateCellProcessor next)
Constructs a new ParseDate processor which converts a String to a Date using the supplied date format and Locale, then calls the next processor in the chain.- Parameters:
dateFormat
- the date format to uselenient
- whether date interpretation is lenientlocale
- the Locale used to parse the datenext
- the next processor in the chain- Throws:
java.lang.NullPointerException
- if dateFormat, locale, or next is null
-
-
Method Detail
-
checkPreconditions
private static void checkPreconditions(java.lang.String dateFormat)
Checks the preconditions for creating a new ParseDate processor with a date format.- Parameters:
dateFormat
- the date format to use- Throws:
java.lang.NullPointerException
- if dateFormat is null
-
checkPreconditions
private static void checkPreconditions(java.lang.String dateFormat, java.util.Locale locale)
Checks the preconditions for creating a new ParseDate processor with date format and locale.- Parameters:
dateFormat
- the date format to uselocale
- the Locale used to parse the date- Throws:
java.lang.NullPointerException
- if dateFormat or locale is null
-
execute
public java.lang.Object execute(java.lang.Object value, CsvContext context)
This method is invoked by the framework when the processor needs to process data or check constraints.- Specified by:
execute
in interfaceCellProcessor
- Parameters:
value
- the value to be processedcontext
- the CSV context- Returns:
- the result of cell processor execution
- Throws:
SuperCsvCellProcessorException
- if value is null, isn't a String, or can't be parsed to a Date
-
-