Return-Path:
* Standard {@link Converter} implementation that converts an incoming String
* into a
* Supported date formats (java.util.Date
object roughly according to the ISO8601
* standard, optionally using a default value or throwing a
* {@link ConversionException} if a conversion error occurs.
* java.text.SimpleDateFormat
patterns):
*
*
*
* This class makes use of java.text.SimpleDateFormat
to parse strings.
* As a result it is not fully compliant with ISO8601, since SimpleDateFormat
* uses a slightly different time zone designator (TZD) than ISO 8601:
*
* ISO 8601: "-00:00" * SimpleDateFormat: "-0000" ** * * @author Flavio Tordini * @version $Id$ * @see http://www.w3.org/TR/NOTE-datetime */ public final class ISO8601DateConverter implements Converter { // ----------------------------------------------------------- Constructors /** * Create a {@link Converter} that will throw a {@link ConversionException} * if a conversion error occurs. */ public ISO8601DateConverter() { this.defaultValue = null; this.useDefault = false; } /** * Create a {@link Converter} that will return the specified default value * if a conversion error occurs. * * @param defaultValue * The default value to be returned */ public ISO8601DateConverter(Object defaultValue) { this.defaultValue = defaultValue; this.useDefault = true; } // ----------------------------------------------------- Instance Variables /** * ISO 8601 date formats. Order is important, must be from more specific to * less specific. */ private static final String[] patterns = { "yyyy-MM-dd'T'HH:mm:ssZ", "yyyy-MM-dd'T'HH:mm:ss", "yyyy-MM-dd'T'HH:mmZ", "yyyy-MM-dd'T'HH:mm", "yyyy-MM-dd", "yyyy-MM", "yyyy",}; /** * the
SimpleDateFormat
object used to parse strings.
*/
private final SimpleDateFormat dateFormat = new SimpleDateFormat();
/**
* The default value specified to our Constructor, if any.
*/
private Object defaultValue = null;
/**
* Should we return the default value on conversion errors?
*/
private boolean useDefault = true;
// --------------------------------------------------------- Public Methods
/**
* Convert the specified input object into an output object of the
* specified type.
*
* @param type
* Data type to which this value should be converted
* @param value
* The input value to be converted
*
* @exception ConversionException
* if conversion cannot be performed successfully
*/
public Object convert(Class type, Object value) {
if (value == null) {
if (useDefault) {
return (defaultValue);
} else {
throw new ConversionException("No value specified");
}
}
if (value instanceof Date) {
return (value);
} else if (value instanceof java.lang.String) {
for (int i = 0; i < patterns.length; i++) {
try {
dateFormat.applyPattern(patterns[i]);
return dateFormat.parse((String) value);
} catch (Exception e) {
// ignore
}
}
if (useDefault) {
return (defaultValue);
} else {
throw new ConversionException("Invalid date format.");
}
}
if (useDefault) {
return (defaultValue);
} else {
throw new ConversionException("Cannot convert.");
}
}
public static final void main(String[] args) {
ConvertUtils.register(new ISO8601DateConverter(), java.util.Date.class);
String myDate = "2004-08-08T04:20:13";
long startTime = System.currentTimeMillis();
Date result = null;
for (int i = 0; i < 1000; i++) {
result = (Date) ConvertUtils.convert(myDate, java.util.Date.class);
}
long processTime = System.currentTimeMillis() - startTime;
System.out.println(result + " time: " + processTime);
}
}
--------------050801050904020904080109
Content-Type: text/plain; charset=us-ascii
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org
--------------050801050904020904080109--