Package com.opencsv.bean
Class ConverterDate
java.lang.Object
com.opencsv.bean.AbstractCsvConverter
com.opencsv.bean.ConverterDate
- All Implemented Interfaces:
CsvConverter
This class converts an input to a date type.
This class should work with any type derived from Date
as 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
Calendar or is derived from
XMLGregorianCalendar. 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:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final Stringprivate final DateTimeFormatterThe formatter for all inputs toTemporalAccessorrepresentations.private final SimpleDateFormatThe formatter for all inputs to old-style date representations.private final BiFunction<DateTimeFormatter, String, TemporalAccessor> A reference to the function to use when converting from strings toTemporalAccessor-based values.private final DateTimeFormatterThe formatter for all outputs fromTemporalAccessorrepresentations.private final SimpleDateFormatThe formatter for all outputs from old-style date representations.private final BiFunction<DateTimeFormatter, TemporalAccessor, String> A 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 -
Method Summary
Modifier and TypeMethodDescriptionconvertToRead(String value) Method for converting from a string to the proper data type of the destination field.convertToWrite(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 ChronologygetChronology(String readChronology, Locale locale2) private SimpleDateFormatsetDateFormat(String format, Locale formatLocale) private DateTimeFormattersetDateTimeFormatter(String format, Locale formatLocale) Methods inherited from class com.opencsv.bean.AbstractCsvConverter
setErrorLocale, setLocale, setType, setWriteLocale
-
Field Details
-
CSVDATE_NOT_DATE
- See Also:
-
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
The formatter for all outputs from old-style date representations. It is absolutely critical that access to this member variable is always synchronized! -
readDtf
The formatter for all inputs toTemporalAccessorrepresentations. -
writeDtf
The formatter for all outputs fromTemporalAccessorrepresentations. -
readTemporalConversionFunction
A reference to the function to use when converting from strings toTemporalAccessor-based values. -
writeTemporalConversionFunction
A reference to the function to use when converting fromTemporalAccessor-based values to strings.
-
-
Constructor Details
-
ConverterDate
public ConverterDate(Class<?> type, String locale, String writeLocale, Locale errorLocale, String readFormat, String writeFormat, String readChronology, 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 populatedlocale- 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 messagesreadFormat- The string to use for parsing the date. SeeCsvDate.value()writeFormat- The string to use for formatting the date. SeeCsvDate.writeFormat()readChronology- TheChronologyto be used for reading ifTemporalAccessor-based fields are in usewriteChronology- TheChronologyto be used for writing ifTemporalAccessor-based fields are in use
-
-
Method Details
-
determineWriteTemporalConversionFunction
private BiFunction<DateTimeFormatter,TemporalAccessor, determineWriteTemporalConversionFunctionString> (Class<?> type) -
determineReadTemporalConversionFunction
private BiFunction<DateTimeFormatter,String, determineReadTemporalConversionFunctionTemporalAccessor> (Class<?> type) -
setDateFormat
-
setDateTimeFormatter
-
getChronology
-
convertToRead
Description 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
This 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
-