Class 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 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
    It is also known to work with org.apache.xerces.jaxp.datatype.XMLGregorianCalendarImpl.

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

    Since:
    4.2 (previously BeanFieldDate since 3.8)
    Author:
    Andrew Rucker Jones
    See Also:
    CsvDate
    • 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.Object convertToRead​(java.lang.String value)
      Method for converting from a string to the proper data type of the destination field.
      java.lang.String convertToWrite​(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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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 populated
        readFormat - The string to use for parsing the date. See CsvDate.value()
        writeFormat - The string to use for formatting the date. See CsvDate.writeFormat()
        locale - If not null or empty, specifies the locale used for converting locale-specific data types
        writeLocale - If not null or empty, specifies the locale used for converting locale-specific data types for writing
        errorLocale - The locale to use for error messages
        readChronology - The Chronology to be used for reading if TemporalAccessor-based fields are in use
        writeChronology - The Chronology to be used for writing if TemporalAccessor-based fields are in use
    • Method Detail

      • convertToRead

        public java.lang.Object convertToRead​(java.lang.String value)
                                       throws CsvDataTypeMismatchException
        Description copied from interface: CsvConverter
        Method 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 be null, empty or blank according to StringUtils.isBlank(java.lang.CharSequence)
        Returns:
        An Object representing 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 CsvDataTypeMismatchException
        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:
        convertToWrite in interface CsvConverter
        Overrides:
        convertToWrite in class AbstractCsvConverter
        Parameters:
        value - The object containing a date of one of the supported types
        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.
        Throws:
        CsvDataTypeMismatchException - If an unsupported type as been improperly annotated