Top Description Fields Constructors Methods
com.opencsv.bean

public Class ConverterDate

extends AbstractCsvConverter
Class Inheritance
Imports
com.opencsv.ICSVParser, com.opencsv.exceptions.CsvBadConverterException, .CsvDataTypeMismatchException, org.apache.commons.lang3.StringUtils, javax.xml.datatype.DatatypeConfigurationException, .DatatypeFactory, .XMLGregorianCalendar, java.lang.reflect.InvocationTargetException, java.text.ParseException, .SimpleDateFormat, java.time.*, java.time.chrono.*, java.time.format.DateTimeFormatter, java.time.temporal.ChronoField, .Temporal, .TemporalAccessor, java.util.*, java.util.function.BiFunction

This class converts an input to a date type.

This class should work with any type derived from java.util.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:

This class should work for any type that implements java.util.Calendar or is derived from javax.xml.datatype.XMLGregorianCalendar. The following types are explicitly supported:

It is also known to work with org.apache.xerces.jaxp.datatype.XMLGregorianCalendarImpl.

This class works for all types from the JDK that implement java.time.temporal.TemporalAccessor.

Author
Andrew Rucker Jones
Since
4.2 (previously BeanFieldDate since 3.8)
See Also
com.opencsv.bean.CsvDate

Field Summary

Modifier and TypeField and Description
private static final String
private final DateTimeFormatter
readDtf

The formatter for all inputs to java.time.temporal.TemporalAccessor representations.

private final SimpleDateFormat
readSdf

The formatter for all inputs to old-style date representations.

private final BiFunction<DateTimeFormatter, String, TemporalAccessor>
readTemporalConversionFunction

A reference to the function to use when converting from strings to java.time.temporal.TemporalAccessor-based values.

private final DateTimeFormatter
writeDtf

The formatter for all outputs from java.time.temporal.TemporalAccessor representations.

private final SimpleDateFormat
writeSdf

The formatter for all outputs from old-style date representations.

private final BiFunction<DateTimeFormatter, TemporalAccessor, String>
writeTemporalConversionFunction

A reference to the function to use when converting from java.time.temporal.TemporalAccessor-based values to strings.

Inherited from com.opencsv.bean.AbstractCsvConverter:
errorLocalelocaletypewriteLocale

Constructor Summary

AccessConstructor and Description
public
ConverterDate(Class<?>
The type of the field being populated
type
,
String
If not null or empty, specifies the locale used for converting locale-specific data types
locale
,
String
If not null or empty, specifies the locale used for converting locale-specific data types for writing
writeLocale
,
Locale
The locale to use for error messages
errorLocale
,
String
The string to use for parsing the date. See com.opencsv.bean.CsvDate#value()
readFormat
,
String
The string to use for formatting the date. See CsvDate#writeFormat()
writeFormat
,
String
The java.time.chrono.Chronology to be used for reading if java.time.temporal.TemporalAccessor-based fields are in use
readChronology
,
String
The java.time.chrono.Chronology to be used for writing if java.time.temporal.TemporalAccessor-based fields are in use
writeChronology
)

Initializes the class.

Method Summary

Modifier and TypeMethod and Description
public Object
convertToRead(String value)

Implements com.opencsv.bean.CsvConverter.convertToRead.

Method for converting from a string to the proper data type of the destination field.

public String

Returns:

A string representation of the date. If a locale or conversion pattern has been specified through annotations, these are used when creating the return value.
convertToWrite
(Object
The object containing a date of one of the supported types
value
)

Overrides com.opencsv.bean.AbstractCsvConverter.convertToWrite.

Implements com.opencsv.bean.CsvConverter.convertToWrite.

This method converts the encapsulated date type to a string, respecting any locales and conversion patterns that have been set through opencsv annotations.

private BiFunction<DateTimeFormatter, String, TemporalAccessor>
private BiFunction<DateTimeFormatter, TemporalAccessor, String>
private Chronology
getChronology(String readChronology, Locale locale2)

private SimpleDateFormat
setDateFormat(String format, Locale formatLocale)

private DateTimeFormatter
setDateTimeFormatter(String format, Locale formatLocale)

Inherited from com.opencsv.bean.AbstractCsvConverter:
setErrorLocalesetLocalesetTypesetWriteLocale

Field Detail

CSVDATE_NOT_DATEback to summary
private static final String CSVDATE_NOT_DATE
readDtfback to summary
private final DateTimeFormatter readDtf

The formatter for all inputs to java.time.temporal.TemporalAccessor representations.

readSdfback to summary
private final 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!

readTemporalConversionFunctionback to summary
private final BiFunction<DateTimeFormatter, String, TemporalAccessor> readTemporalConversionFunction

A reference to the function to use when converting from strings to java.time.temporal.TemporalAccessor-based values.

writeDtfback to summary
private final DateTimeFormatter writeDtf

The formatter for all outputs from java.time.temporal.TemporalAccessor representations.

writeSdfback to summary
private final 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!

writeTemporalConversionFunctionback to summary
private final BiFunction<DateTimeFormatter, TemporalAccessor, String> writeTemporalConversionFunction

A reference to the function to use when converting from java.time.temporal.TemporalAccessor-based values to strings.

Constructor Detail

ConverterDateback to summary
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:Class<?>

The type of the field being populated

locale:String

If not null or empty, specifies the locale used for converting locale-specific data types

writeLocale:String

If not null or empty, specifies the locale used for converting locale-specific data types for writing

errorLocale:Locale

The locale to use for error messages

readFormat:String

The string to use for parsing the date. See com.opencsv.bean.CsvDate#value()

writeFormat:String

The string to use for formatting the date. See CsvDate#writeFormat()

readChronology:String

The java.time.chrono.Chronology to be used for reading if java.time.temporal.TemporalAccessor-based fields are in use

writeChronology:String

The java.time.chrono.Chronology to be used for writing if java.time.temporal.TemporalAccessor-based fields are in use

Method Detail

convertToReadback to summary
public Object convertToRead(String value) throws CsvDataTypeMismatchException

Implements com.opencsv.bean.CsvConverter.convertToRead.

Doc from com.opencsv.bean.CsvConverter.convertToRead.

Method for converting from a string to the proper data type of the destination field.

Annotations
@Override
Exceptions
CsvDataTypeMismatchException:
If the input string cannot be converted into the proper type
convertToWriteback to summary
public String convertToWrite(Object value) throws CsvDataTypeMismatchException

Overrides com.opencsv.bean.AbstractCsvConverter.convertToWrite.

Implements com.opencsv.bean.CsvConverter.convertToWrite.

This method converts the encapsulated date type to a string, respecting any locales and conversion patterns that have been set through opencsv annotations.

Parameters
value:Object

The object containing a date of one of the supported types

Returns:String

A string representation of the date. If a locale or conversion pattern has been specified through annotations, these are used when creating the return value.

Annotations
@Override
Exceptions
CsvDataTypeMismatchException:
If an unsupported type as been improperly annotated
determineReadTemporalConversionFunctionback to summary
private BiFunction<DateTimeFormatter, String, TemporalAccessor> determineReadTemporalConversionFunction(Class<?> type)
determineWriteTemporalConversionFunctionback to summary
private BiFunction<DateTimeFormatter, TemporalAccessor, String> determineWriteTemporalConversionFunction(Class<?> type)
getChronologyback to summary
private Chronology getChronology(String readChronology, Locale locale2)
setDateFormatback to summary
private SimpleDateFormat setDateFormat(String format, Locale formatLocale)
setDateTimeFormatterback to summary
private DateTimeFormatter setDateTimeFormatter(String format, Locale formatLocale)