Example usage for org.apache.commons.beanutils.converters SqlDateConverter convert

List of usage examples for org.apache.commons.beanutils.converters SqlDateConverter convert

Introduction

In this page you can find the example usage for org.apache.commons.beanutils.converters SqlDateConverter convert.

Prototype

public Object convert(Class type, Object value) 

Source Link

Document

Convert the specified input object into an output object of the specified type.

Usage

From source file:org.kuali.kfs.module.ar.batch.vo.CustomerDigesterAdapter.java

/**
 * //from w  w  w .j a v a  2s  .  c  om
 * This method converts a string value that may represent a date into a java.sql.Date.
 * If the value is blank (whitespace, empty, null) then a null Date object is returned.  If 
 * the value cannot be converted to a java.sql.Date, then a RuntimException or ConversionException 
 * will be thrown.
 * 
 * @param propertyName Name of the field whose value is being converted.
 * @param dateValue The value being converted.
 * @param errorMap The errorMap to add conversion errors to.
 * @return A valid java.sql.Date with the converted value, if possible.
 */
private java.sql.Date convertToJavaSqlDate(String propertyName, String dateValue) {

    if (StringUtils.isBlank(dateValue)) {
        return null;
    }

    java.sql.Date date = null;
    SqlDateConverter converter = new SqlDateConverter();
    Object obj = null;
    try {
        obj = converter.convert(java.sql.Date.class, dateValue);
    } catch (ConversionException e) {
        LOG.error("Failed to convert the value [" + dateValue + "] from field [" + propertyName
                + "] to a java.sql.Date.");
        addError(propertyName, java.sql.Date.class, dateValue, "Could not convert value to target type.");
        return null;
    }
    try {
        date = (java.sql.Date) obj;
    } catch (Exception e) {
        LOG.error("Failed to cast the converters results to a java.sql.Date.");
        addError(propertyName, java.sql.Date.class, dateValue, "Could not convert value to target type.");
        return null;
    }

    if (!(obj instanceof java.sql.Date)) {
        LOG.error("Failed to convert the value [" + dateValue + "] from field [" + propertyName
                + "] to a java.sql.Date.");
        addError(propertyName, java.sql.Date.class, dateValue, "Could not convert value to target type.");
        return null;
    }
    return date;
}