Package com.opencsv.bean
Class ConverterDate
- java.lang.Object
-
- com.opencsv.bean.AbstractCsvConverter
-
- com.opencsv.bean.ConverterDate
-
- All Implemented Interfaces:
CsvConverter
public class ConverterDate extends AbstractCsvConverter
This class converts an input to a date type.This class should work with any type derived from
Dateas long as it has a constructor taking one long that specifies the number of milliseconds since the epoch. The following types are explicitly supported:- java.util.Date
- java.sql.Date
- java.sql.Time
- java.sql.Timestamp
This class should work for any type that implements
Calendaror is derived fromXMLGregorianCalendar. The following types are explicitly supported:- Calendar (always a GregorianCalendar)
- GregorianCalendar
- XMLGregorianCalendar
This class works for all types from the JDK that implement
TemporalAccessor.- Since:
- 4.2 (previously BeanFieldDate since 3.8)
- See Also:
CsvDate
-
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.StringCSVDATE_NOT_DATEprivate java.time.format.DateTimeFormatterreadDtfThe formatter for all inputs toTemporalAccessorrepresentations.private java.text.SimpleDateFormatreadSdfThe formatter for all inputs to old-style date representations.private java.util.function.BiFunction<java.time.format.DateTimeFormatter,java.lang.String,java.time.temporal.TemporalAccessor>readTemporalConversionFunctionA reference to the function to use when converting from strings toTemporalAccessor-based values.private java.time.format.DateTimeFormatterwriteDtfThe formatter for all outputs fromTemporalAccessorrepresentations.private java.text.SimpleDateFormatwriteSdfThe formatter for all outputs from old-style date representations.private java.util.function.BiFunction<java.time.format.DateTimeFormatter,java.time.temporal.TemporalAccessor,java.lang.String>writeTemporalConversionFunctionA reference to the function to use when converting fromTemporalAccessor-based values to strings.-
Fields inherited from class com.opencsv.bean.AbstractCsvConverter
errorLocale, locale, type, writeLocale
-
-
Constructor Summary
Constructors Constructor Description ConverterDate(java.lang.Class<?> type, java.lang.String locale, java.lang.String writeLocale, java.util.Locale errorLocale, java.lang.String readFormat, java.lang.String writeFormat, java.lang.String readChronology, java.lang.String writeChronology)Initializes the class.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.ObjectconvertToRead(java.lang.String value)Method for converting from a string to the proper data type of the destination field.java.lang.StringconvertToWrite(java.lang.Object value)This method converts the encapsulated date type to a string, respecting any locales and conversion patterns that have been set through opencsv annotations.private java.util.function.BiFunction<java.time.format.DateTimeFormatter,java.lang.String,java.time.temporal.TemporalAccessor>determineReadTemporalConversionFunction(java.lang.Class<?> type)private java.util.function.BiFunction<java.time.format.DateTimeFormatter,java.time.temporal.TemporalAccessor,java.lang.String>determineWriteTemporalConversionFunction(java.lang.Class<?> type)private java.time.chrono.ChronologygetChronology(java.lang.String readChronology, java.util.Locale locale2)private java.text.SimpleDateFormatsetDateFormat(java.lang.String format, java.util.Locale formatLocale)private java.time.format.DateTimeFormattersetDateTimeFormatter(java.lang.String format, java.util.Locale formatLocale)-
Methods inherited from class com.opencsv.bean.AbstractCsvConverter
setErrorLocale, setLocale, setType, setWriteLocale
-
-
-
-
Field Detail
-
CSVDATE_NOT_DATE
private static final java.lang.String CSVDATE_NOT_DATE
- See Also:
- Constant Field Values
-
readSdf
private final java.text.SimpleDateFormat readSdf
The formatter for all inputs to old-style date representations. It is absolutely critical that access to this member variable is always synchronized!
-
writeSdf
private final java.text.SimpleDateFormat writeSdf
The formatter for all outputs from old-style date representations. It is absolutely critical that access to this member variable is always synchronized!
-
readDtf
private final java.time.format.DateTimeFormatter readDtf
The formatter for all inputs toTemporalAccessorrepresentations.
-
writeDtf
private final java.time.format.DateTimeFormatter writeDtf
The formatter for all outputs fromTemporalAccessorrepresentations.
-
readTemporalConversionFunction
private final java.util.function.BiFunction<java.time.format.DateTimeFormatter,java.lang.String,java.time.temporal.TemporalAccessor> readTemporalConversionFunction
A reference to the function to use when converting from strings toTemporalAccessor-based values.
-
writeTemporalConversionFunction
private final java.util.function.BiFunction<java.time.format.DateTimeFormatter,java.time.temporal.TemporalAccessor,java.lang.String> writeTemporalConversionFunction
A reference to the function to use when converting fromTemporalAccessor-based values to strings.
-
-
Constructor Detail
-
ConverterDate
public ConverterDate(java.lang.Class<?> type, java.lang.String locale, java.lang.String writeLocale, java.util.Locale errorLocale, java.lang.String readFormat, java.lang.String writeFormat, java.lang.String readChronology, java.lang.String writeChronology)Initializes the class. This includes initializing the locales for reading and writing, the format strings for reading and writing, and the chronologies for reading and writing, all as necessary based on the type to be converted.- Parameters:
type- The type of the field being populatedreadFormat- The string to use for parsing the date. SeeCsvDate.value()writeFormat- The string to use for formatting the date. SeeCsvDate.writeFormat()locale- If not null or empty, specifies the locale used for converting locale-specific data typeswriteLocale- If not null or empty, specifies the locale used for converting locale-specific data types for writingerrorLocale- The locale to use for error messagesreadChronology- TheChronologyto be used for reading ifTemporalAccessor-based fields are in usewriteChronology- TheChronologyto be used for writing ifTemporalAccessor-based fields are in use
-
-
Method Detail
-
determineWriteTemporalConversionFunction
private java.util.function.BiFunction<java.time.format.DateTimeFormatter,java.time.temporal.TemporalAccessor,java.lang.String> determineWriteTemporalConversionFunction(java.lang.Class<?> type)
-
determineReadTemporalConversionFunction
private java.util.function.BiFunction<java.time.format.DateTimeFormatter,java.lang.String,java.time.temporal.TemporalAccessor> determineReadTemporalConversionFunction(java.lang.Class<?> type)
-
setDateFormat
private java.text.SimpleDateFormat setDateFormat(java.lang.String format, java.util.Locale formatLocale)
-
setDateTimeFormatter
private java.time.format.DateTimeFormatter setDateTimeFormatter(java.lang.String format, java.util.Locale formatLocale)
-
getChronology
private java.time.chrono.Chronology getChronology(java.lang.String readChronology, java.util.Locale locale2)
-
convertToRead
public java.lang.Object convertToRead(java.lang.String value) throws CsvDataTypeMismatchExceptionDescription copied from interface:CsvConverterMethod for converting from a string to the proper data type of the destination field.- Parameters:
value- The string from the selected field of the CSV file. If the field is marked as required in the annotation, this value is guaranteed not to benull, empty or blank according toStringUtils.isBlank(java.lang.CharSequence)- Returns:
- An
Objectrepresenting the input data converted into the proper type - Throws:
CsvDataTypeMismatchException- If the input string cannot be converted into the proper type
-
convertToWrite
public java.lang.String convertToWrite(java.lang.Object value) throws CsvDataTypeMismatchExceptionThis method converts the encapsulated date type to a string, respecting any locales and conversion patterns that have been set through opencsv annotations.- Specified by:
convertToWritein interfaceCsvConverter- Overrides:
convertToWritein classAbstractCsvConverter- Parameters:
value- The object containing a date of one of the supported types- Returns:
- A string representation of the date. If a
localeorconversion patternhas been specified through annotations, these are used when creating the return value. - Throws:
CsvDataTypeMismatchException- If an unsupported type as been improperly annotated
-
-