public String format(Object dataToFormat) throws JspException
{
if (dataToFormat == null) {
return null;
}
InternalStringBuilder formattedString = new InternalStringBuilder(32);
SimpleDateFormat dateFormat = null;
if (getPattern() != null) {
try {
if (locale != null) {
dateFormat = new SimpleDateFormat(getPattern(), locale);
}
else {
dateFormat = new SimpleDateFormat(getPattern());
}
}
catch (IllegalArgumentException e) {
String s = Bundle.getString("Tags_DateFormatPatternException", new Object[]{e.getMessage()});
logger.warn(s);
throw new JspException(s);
}
}
else {
dateFormat = new SimpleDateFormat();
}
if (dataToFormat instanceof java.sql.Date) {
java.sql.Date date = (java.sql.Date) dataToFormat;
formattedString.append(dateFormat.format(date));
}
else if (dataToFormat instanceof java.util.Date) {
java.util.Date date = (java.util.Date) dataToFormat;
formattedString.append(dateFormat.format(date));
}
else if (dataToFormat instanceof java.util.Calendar) {
java.util.Calendar c = (java.util.Calendar) dataToFormat;
java.util.Date date = new java.util.Date(c.getTimeInMillis());
formattedString.append(dateFormat.format(date));
}
else if (dataToFormat instanceof String) {
if (dataToFormat.equals("")) {
return "";
}
DateFormat df = null;
if (inputPattern != null) {
try {
df = new SimpleDateFormat(inputPattern);
}
catch (IllegalArgumentException e) {
String s = Bundle.getString("Tags_formatDate_StringPatternError",
new Object[]{inputPattern, e.getMessage()});
logger.warn(s);
throw new JspException(s);
}
// let try and convert this to some type of date
java.util.Date date = df.parse((String) dataToFormat,
new ParsePosition(0));
if (date != null) {
formattedString.append(dateFormat.format(date));
return formattedString.toString();
}
}
// this will loop through all of the formats and
// try to convert the date to one of them.
int i;
for (i = 0; i < commonFormats.length; i++) {
if (commonFormats[i] != null) {
df = new SimpleDateFormat(commonFormats[i]);
}
else {
df = new SimpleDateFormat();
}
// let try and convert this to some type of date
java.util.Date date = df.parse((String) dataToFormat,
new ParsePosition(0));
if (date != null) {
formattedString.append(dateFormat.format(date));
break;
}
}
if (i == commonFormats.length) {
String s = Bundle.getString("Tags_formatDate_String_Error",
new Object[]{dataToFormat});
logger.error(s);
throw new JspException(s);
}
}
else {
String s = Bundle.getString("Tags_formatDate_Type_Error",
new Object[]{dataToFormat.getClass().getName()});
logger.error(s);
throw new JspException(s);
}
return formattedString.toString();
}