Ejemplo n.º 1
0
  /**
   * Convert a date to a String and a String to a Date
   *
   * @param type String, Date or Timestamp
   * @param value value to convert
   * @return Converted value for property population
   */
  public Object convert(Class type, Object value) {
    if (value == null) {
      return null;
    } else if (type == Timestamp.class) {
      return convertToDate(type, value, DateUtil.getDateTimePattern());
    } else if (type == Date.class) {
      return convertToDate(type, value, DateUtil.getDatePattern());
    } else if (type == String.class) {
      return convertToString(type, value);
    }

    throw new ConversionException(
        "Could not convert " + value.getClass().getName() + " to " + type.getName());
  }
Ejemplo n.º 2
0
/**
 * This class is converts a java.util.Date to a String and a String to a java.util.Date for use as a
 * Timestamp. It is used by BeanUtils when copying properties.
 *
 * @author <a href="mailto:[email protected]">Dan Kibler</a>
 */
public class TimestampConverter extends DateConverter {
  /** i18n-ized timestamp format - based on values in ApplicationResources.properties */
  public static final String TS_FORMAT = DateUtil.getDatePattern() + " HH:mm:ss.S";

  /**
   * Convert a String to a date
   *
   * @param type java.util.Date
   * @param value the String value
   * @return a converted date
   */
  protected Object convertToDate(Class type, Object value) {
    DateFormat df = new SimpleDateFormat(TS_FORMAT);
    if (value instanceof String) {
      try {
        if (StringUtils.isEmpty(value.toString())) {
          return null;
        }

        return df.parse((String) value);
      } catch (Exception pe) {
        throw new ConversionException("Error converting String to Timestamp");
      }
    }

    throw new ConversionException(
        "Could not convert " + value.getClass().getName() + " to " + type.getName());
  }

  /**
   * Convert from a java.util.Date to a String
   *
   * @param type java.lang.String
   * @param value the date instance
   * @return string version of date using default date pattern
   */
  protected Object convertToString(Class type, Object value) {
    DateFormat df = new SimpleDateFormat(TS_FORMAT);
    if (value instanceof Date) {
      try {
        return df.format(value);
      } catch (Exception e) {
        throw new ConversionException("Error converting Timestamp to String");
      }
    }

    return value.toString();
  }
}
Ejemplo n.º 3
0
  /**
   * Convert a java.util.Date or a java.sql.Timestamp to a String. Or does a toString
   *
   * @param value value to convert
   * @return Converted value for property population
   */
  protected Object convertToString(final Object value) {
    if (value instanceof Date) {
      DateFormat df = new SimpleDateFormat(DateUtil.getDatePattern());
      if (value instanceof Timestamp) {
        df = new SimpleDateFormat(DateUtil.getDateTimePattern());
      }

      try {
        return df.format(value);
      } catch (final Exception e) {
        throw new ConversionException("Error converting Date to String", e);
      }
    } else {
      return value.toString();
    }
  }
Ejemplo n.º 4
0
  /**
   * Convert a java.util.Date to a String
   *
   * @param type Date or Timestamp
   * @param value value to convert
   * @return Converted value for property population
   */
  protected Object convertToString(Class type, Object value) {

    if (value instanceof Date) {
      DateFormat df = new SimpleDateFormat(DateUtil.getDatePattern());
      if (value instanceof Timestamp) {
        df = new SimpleDateFormat(DateUtil.getDateTimePattern());
      }

      try {
        return df.format(value);
      } catch (Exception e) {
        e.printStackTrace();
        throw new ConversionException("Error converting Date to String");
      }
    } else {
      return value.toString();
    }
  }