/** * This method converts the encapsulated date type to a string, respecting * any locales and conversion patterns that have been set through opencsv * annotations. * * @param value The object containing a date of one of the supported types * @return A string representation of the date. If a * {@link CsvBindByName#locale() locale} or {@link CsvDate#value() 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 */ @Override public String convertToWrite(Object value) throws CsvDataTypeMismatchException { return value == null ? null : convertCommon(value, String.class); } }
/** * Splits the conversion into date-based and calendar-based. * * @param <U> The type to be converted to * @param value The string to be converted into a date/time type or vice * versa * @param fieldType The class of the destination field * @return The object resulting from the conversion * @throws CsvDataTypeMismatchException If a non-convertible type is * passed in, or if the conversion fails */ private <U> U convertCommon(Object value, Class<U> fieldType) throws CsvDataTypeMismatchException { U o; Class conversionClass = (fieldType == String.class)?value.getClass():fieldType; // Send to the proper sub-method if (Date.class.isAssignableFrom(conversionClass)) { o = convertDate(value, fieldType); } else if (Calendar.class.isAssignableFrom(conversionClass) || XMLGregorianCalendar.class.isAssignableFrom(conversionClass)) { o = convertCalendar(value, fieldType); } else { throw new CsvDataTypeMismatchException(value, fieldType, ResourceBundle.getBundle(ICSVParser.DEFAULT_BUNDLE_NAME, errorLocale).getString(CSVDATE_NOT_DATE)); } return o; }
converter = new ConverterDate(elementType, locale, errorLocale, formatString);
@Override public Object convertToRead(String value) throws CsvDataTypeMismatchException { return StringUtils.isBlank(value) ? null : convertCommon(value, type); }